替换jtable的内容

时间:2013-03-16 13:52:58

标签: java swing netbeans jtable

我有一个使用NetBeans IDE的桌面应用程序  应用程序显示JTable  来自lucene搜索操作的数据。

每次进行新搜索时,  该表将新搜索结果附加到  以前的搜索结果。我是什么 希望是表替换任何退出 使用新的搜索结果搜索结果。为了 表格的单词刷新并显示新的  搜索结果。

任何可用的建议

这是datamodel的代码片段

public class MyTableModel extends AbstractTableModel {

    private Vector<Vector<String>> dataList = new Vector<>();
     private String[] header = { "ID","SUBJECT","LETTTER FROM","LETTER DATE","DATE RECEIED",
                                  "REMARKS","DATE DISPATCHED","DESTINATION OFFICE"};



    public Vector<Vector<String>> getDataList() {
        return dataList;
    }

    public void setDataList(Vector<Vector<String>> dataList) {       
        this.dataList = dataList;
        fireTableDataChanged(); 
    }

    public void setHeader(String[] header) {
        this.header = header;
    }

    public String[] getHeader() {
        return header;
    }

    @Override
    public int getRowCount() {
        return dataList.size();
    }

    @Override
    public int getColumnCount() {
        return header.length;
    }

    @Override
   public String getColumnName(int col) {
    return header[col];
   }

    @Override
    public Object getValueAt(int row, int col) { 
    return dataList.get(row).get(col);
    }

}

此代码将搜索结果传递给数据模型类

private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
    try {

        searchField = searchTextField.getText();
        if(!searchField.isEmpty())
        {                
       matrix = dbs.searchDatabase(searchField + "*");
       myModel.setDataList(matrix);

        }

    } catch (CorruptIndexException ex) {
        Logger.getLogger(GNSSJFrame.class.getName()).log(Level.SEVERE, null, ex);
    } catch (LockObtainFailedException ex) {
        Logger.getLogger(GNSSJFrame.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException | ParseException ex) {
        Logger.getLogger(GNSSJFrame.class.getName()).log(Level.SEVERE, null, ex);
    }             
}

1 个答案:

答案 0 :(得分:4)

如果您的桌面模型是这样的,

class AllTableModel extends AbstractTableModel {

    // Suppose this is the data list table is using,
    List<TableData> tableData = new ArrayList<TableData>();

    // Override methods goes here.

    public void setTableData(List<TableData> tableData) {
        this.tableData = tableData;
        fireTableDataChanged();
}  
}

现在,使用表模型实例将新数据设置到列表中。

 allTableModel.setTableData(/* Set new search results to the list.*/);