create TableModel and populate jTable dynamically

Julia picture Julia · May 30, 2010 · Viewed 33.6k times · Source

I want to store the results of reading lucene index into jTable, so that I can make it sortable by different columns. From index I am reading terms with different measures of their frequencies.

Table columns are these : [string term][int absFrequency][int docFrequency][double invFrequency]

So i in AbstractTableModel I can define column names, but i dont know how to get the Object[][]data with 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's 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 am kindda stuck here to proceed to implement AbstractTableModel and populate and display this table .... :/

Please help!

Answer

Jonas picture Jonas · May 30, 2010

When you are inserting, deleting or updating data in your model, you need to notify the GUI of the changes. You can do this with the fire-methods in the AbstractTableModel.

I.e. if you add an element to your list, you also have to call fireTableRowsInserted(int firstRow, int lastRow) so that the visible layer can be updated.

Have a look at 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;
    }

}