我有一个创建表的类:
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.util.*;
public class ItemsTable extends JPanel {
ArrayList < Item > items = new ArrayList < Item > ();
ItemsTable(ArrayList < Item > items, Integer choice, String search) {
super(new GridLayout(1, 0));
this.items = items;
String[] columnNames = {
"No.",
"Title",
"Description",
"Keyword",
"User ID",
"Start time",
"Close time",
"Reserve Price",
"ID code"
};
DefaultTableModel model = new DefaultTableModel(0, 0);
JTable table = new JTable();
model.setColumnIdentifiers(columnNames);
table.setModel(model);
int k = 0;
for (Item i: items) {
if ((choice == 0 && i.getUserID().equals(search)) || (choice == 1 && i.getKeyword().equals(search)) || (choice == 2 && i.getIdCode().equals(search))) {
k++;
Object[] newRow = {
k, i.getTitle(), i.getDescription(), i.getKeyword(), i.getUserID(), i.getStartTime(), i.getCloseTime(), i.getReservePrice(), i.getIdCode()
};
model.addRow(newRow);
}
}
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}
}
我想在表格中搜索一些值,选择我要查找的列并搜索该列中的字符串。
在另一个班级,我有一个搜索按钮:
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Integer choice = searchBox.getSelectedIndex();
System.out.println(choice);
ItemsTable table = new ItemsTable(items, choice, searchField.getText());
//searchPanel.add(table, BorderLayout.SOUTH);
}
});
但是在我搜索一次之后,下次我要搜索表格时不会改变。我该如何更新或刷新它?