我有一个使用NetBeans IDE的swing桌面应用程序应用程序有一个显示的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);
}
}
如果您的表模型是这样的,
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.*/);