简体   繁体   English

替换jtable的内容

[英]replacing the content of a jtable

I have a desktop application in swing with NetBeans IDE the application has a JTable that displays data from a lucene search operation. 我有一个使用NetBeans IDE的桌面应用程序,该应用程序有一个JTable ,它显示来自lucene搜索操作的数据。

Any time a new search is made , the table appends the new search results to the previous search result. 每次进行新搜索时,该表都会将新搜索结果附加到上一个搜索结果中。 What I want is for the table to replace any exiting search results with the new search results. 我想要的是表格用新的搜索结果替换任何现有的搜索结果。 In order words for the table to refresh and display the new search results. 按顺序为表格刷新并显示新的搜索结果。

Any suggestions available 任何建议

this is the code snippet for the 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);
    }

}

this code passes the search result to the data model class 此代码将搜索结果传递给数据模型类

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

If your table model is in this way, 如果您的桌面模型是这样的,

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

Now, set the new data to the list using the table model instance. 现在,使用表模型实例将新数据设置到列表中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM