Create TableModel and dynamically populate jTable

I want to save the results of reading the lucene index in jTable so that I can sort it by different columns. From the index I read terms with various measurements of their frequencies.

Column columns: [string term] [int absFrequency] [int docFrequency] [double invFrequency]

So, in AbstractTableModel I can define the column names, but I don’t know how to get the Object [] [] data with the results from the following method:

public static void FrequencyMap(Directory indexDir) throws Exception { List<ArrayList>redoviLista = new ArrayList<ArrayList>(); //final Map<String,TermRow> map = new TreeMap<String,TermRow>(); List<String>termList = new ArrayList<String>(); IndexReader iReader = IndexReader.open(indexDir); FilterIndexReader fReader = new FilterIndexReader(iReader); int numOfDocs = fReader.numDocs(); TermEnum terms = fReader.terms(); while (terms.next()){ Term term = terms.term(); String termText = term.text(); termList.add(termText); //Calculating the frequencies int df = iReader.docFreq(term); double idf = 0.0F; idf = Math.log10((double) numOfDocs / df); double tfidf = (df*idf); //Here comes important part //Changes according to takoi answer ArrayList<Object> oneRow = new ArrayList<Object>(); oneRow.add(termText); oneRow.add(df); oneRow.add(idf); oneRow.add(tfidf); redoviLista.add(oneRow); } iReader.close(); // So I need something like this, and i Neeed this array to be stored out of this method 

So, I became famous here to continue the implementation of AbstractTableModel and populate and display this table ....: /

Please, help!

+6
java swing jtable tablemodel abstracttablemodel
source share
3 answers

When you insert, delete, or update data in your model, you need to notify the graphical interface about this. You can do this using fire methods in AbstractTableModel .

those. if you add an item to your list, you must also call fireTableRowsInserted(int firstRow, int lastRow) to update the visible level.

See addElement(MyElement e) in the code below:

 public class MyModel extends AbstractTableModel { private static final String[] columnNames = {"column 1", "column 2"}; private final LinkedList<MyElement> list; private MyModel() { list = new LinkedList<MyElement>(); } public void addElement(MyElement e) { // Adds the element in the last position in the list list.add(e); fireTableRowsInserted(list.size()-1, list.size()-1); } @Override public int getColumnCount() { return columnNames.length; } @Override public int getRowCount() { return list.size(); } @Override public Object getValueAt(int rowIndex, int columnIndex) { switch(columnIndex) { case 0: return list.get(rowIndex).getColumnOne(); case 1: return list.get(rowIndex).getColumnOne(); } return null; } } 
+5
source share

There is no need to create a custom Model table for this. Just use DefaultListModel.

DefaultListModel allows you to dynamically add rows to the model using the addRow (...) method, and it automatically calls the appropriate fireXXX method to show that the table is redrawing itself.

Base Code:

 DefaultTableModel model = new DefaultTableModel( columnNames ); while (...) { Vector row = new Vector(); row.add(...) row.add(...) model.addRow( row ); } JTable table = new JTable( model ); 

You do not need to create a custom TableModel every time. Sometimes you can start with DefaultTableModel.

+2
source share

You do not need to create an object [] []. Just make the redoviLista list a list:

 redoviLista.add( new ArrayList<Object>(termText, df, idf, tfidf) ); #pseudocode 

then you implement getValueAt as follows:

 getValueAt(int row, int column){ redoviLista.get(row).get(column); } 
+1
source share

All Articles