java – 创建TableModel并动态填充jTable

前端之家收集整理的这篇文章主要介绍了java – 创建TableModel并动态填充jTable前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想将读取lucene索引的结果存储到jTable中,以便我可以通过不同的列对其进行排序.从索引我正在阅读具有不同频率度量的术语.

表列是这些:
[string term] [int absFrequency] [int docFrequency] [double invFrequency]

所以我在AbstractTableModel中我可以定义列名,但我不知道如何使用以下方法得到Object [] []数据:

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

所以我很友好地坚持在这里继续实现AbstractTableModel并填充并显示这个表….:/

请帮忙!

解决方法

在模型中插入,删除或更新数据时,需要通知GUI更改.您可以使用 AbstractTableModel中的fire-methods执行此操作.

即如果向列表中添加元素,则还必须调用fireTableRowsInserted(int firstRow,int lastRow),以便可以更新可见图层.

在下面的代码中查看addElement(MyElement e):

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;
    }

}

猜你在找的Java相关文章