Java 创建 TableModel 并动态填充 jTable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2937991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
create TableModel and populate jTable dynamically
提问by Julia
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.
我想将读取lucene索引的结果存入jTable,这样我就可以按不同的列对其进行排序。从索引中,我正在阅读具有不同频率度量的术语。
Table columns are these : [string term][int absFrequency][int docFrequency][double invFrequency]
表列是这些: [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:
所以我在 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
So I am kindda stuck here to proceed to implement AbstractTableModel and populate and display this table .... :/
所以我有点卡在这里继续实现 AbstractTableModel 并填充和显示这个表......:/
Please help!
请帮忙!
采纳答案by Jonas
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
.
当您在模型中插入、删除或更新数据时,您需要将更改通知 GUI。你可以用做火的-方法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.
即,如果您将一个元素添加到您的列表中,您还必须调用fireTableRowsInserted(int firstRow, int lastRow)
以便可以更新可见层。
Have a look at addElement(MyElement e)
in the code below:
看看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;
}
}
回答by gwohpq9
You dont have to create an Object[][]. Just make your redoviLista a list of lists:
您不必创建 Object[][]。只需让您的 redoviLista 成为列表列表:
redoviLista.add( new ArrayList<Object>(termText, df, idf, tfidf) ); #pseudocode
then you implement getValueAt like this:
然后你实现 getValueAt 像这样:
getValueAt(int row, int column){
redoviLista.get(row).get(column);
}
回答by camickr
There is no need to create a custom TableModel for this. Just use the DefaultListModel.
无需为此创建自定义 TableModel。只需使用 DefaultListModel。
The DefaultListModel allows you to dynamcially add rows to the model using the addRow(...) method and it automatically invokes the appropriate fireXXX method to tell the table to repaint itself.
DefaultListModel 允许您使用 addRow(...) 方法动态地将行添加到模型中,并且它会自动调用适当的 fireXXX 方法来告诉表重新绘制自身。
The basic code would be:
基本代码是:
DefaultTableModel model = new DefaultTableModel( columnNames );
while (...)
{
Vector row = new Vector();
row.add(...)
row.add(...)
model.addRow( row );
}
JTable table = new JTable( model );
You don't need to create a custom TableModel everytime. Sometimes you can start with the DefaultTableModel.
您不需要每次都创建自定义 TableModel。有时您可以从 DefaultTableModel 开始。