1

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

每次进行新搜索时,该表都会将新搜索结果附加到先前的搜索结果中。我想要的是表格用新的搜索结果替换任何现有的搜索结果。为了让表中的词刷新并显示新的搜索结果。

任何可用的建议

这是数据模型的代码片段

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

1 回答 1

5

如果你的表模型是这样的,

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.*/);
于 2013-03-16T14:23:49.090 回答