创建TableModel并动态填充jTable [英] create TableModel and populate jTable dynamically

查看:116
本文介绍了创建TableModel并动态填充jTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将读取lucene索引的结果存储到jTable中,以便我可以通过不同的列对其进行排序。从索引我正在读取具有不同频率度量的术语。

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.

表格列如下:
[string term] [int absFrequency] [int docFrequency] [ double invFrequency]

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

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

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

所以我很友好地继续实施AbstractTableModel并填充并显示这个表....:/

So I am kindda stuck here to proceed to implement AbstractTableModel and populate and display this table .... :/

请帮忙!

推荐答案

在模型中插入,删除或更新数据时,需要通知GUI更改。您可以使用 fire 方法执行此操作.htmlrel =noreferrer> AbstractTableModel

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.

即如果你在列表中添加一个元素,你还必须调用 fireTableRowsInserted(int firstRow,int lastRow),以便可以更新可见层。

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.

在下面的代码中查看 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;
    }

}

这篇关于创建TableModel并动态填充jTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆