import javax.swing.; import javax.swing.table.DefaultTableModel; import java.awt.; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Vector; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; import javax.swing.table.TableRowSorter; public class TableFilterDemo extends JFrame { private JComboBox<String> combobox_category, comboBox_category1; private JComboBox<String> comboBox_type, comboBox_type1; private Vector<Computer> computer; public TableFilterDemo(Vector<Computer> computer) { this.computer = computer; DefaultTableModel tableModel = new DefaultTableModel(); Vector<String> columns = new Vector<>(); columns.add("Category"); columns.add("Type"); columns.add("ID"); columns.add("Brand"); columns.add("CPU Family"); columns.add("Price($)"); JTable jTable = new JTable(tableModel); JFrame F = new JFrame(); F.setLayout(new GridLayout(3,1)); F.add(jTable); Vector<Vector<String>> rows = new Vector<Vector<String>>(); for (int i = 0; i < computer.size(); i++) { Vector<String> row = new Vector<String>(); row.add(computer.get(i).category); row.add(computer.get(i).Type); row.add(computer.get(i).ID); row.add(computer.get(i).Brand); row.add(computer.get(i).CPU_Family); row.add(computer.get(i).Price + ".0($)"); rows.add(row); } tableModel.setDataVector(rows, columns); JComboBox<String> categoryCombo = new JComboBox<String>(); categoryCombo.addItem("All"); categoryCombo.addItem("Desktop PC"); categoryCombo.addItem("Laptop"); categoryCombo.addItem("Tablet"); JComboBox<String> typeCombo = new JComboBox<String>(); typeCombo.addItem("All"); typeCombo.addItem("Home & Study"); typeCombo.addItem("Business"); typeCombo.addItem("Compact"); typeCombo.addItem("Thin & Light"); typeCombo.addItem("Android"); typeCombo.addItem("Apple"); typeCombo.addItem("Windows"); final String[] categoryFilter = {(String) categoryCombo.getSelectedItem()}; final String[] typeFilter = {(String) typeCombo.getSelectedItem()}; TableRowSorter tableRowSorter = new TableRowSorter<>(jTable.getModel()); RowFilter<Object, Object> compoundRowFilter = new RowFilter<Object, Object>() { public boolean include(Entry<?, ?> entry) { Object categoryValue = entry.getValue(0); Object typeValue = entry.getValue(1); return (categoryFilter[0].equals("All") || categoryFilter[0].equals(categoryValue)) && (typeFilter[0].equals("All") || typeFilter[0].equals(typeValue)); } }; categoryCombo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { categoryFilter[0] = (String) categoryCombo.getSelectedItem(); tableRowSorter.setRowFilter(compoundRowFilter); } }); typeCombo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { typeFilter[0] = (String) typeCombo.getSelectedItem(); tableRowSorter.setRowFilter(compoundRowFilter); } }); JPanel detailsPanel = new JPanel(new GridLayout(12, 2, 10, 10)); F.add(categoryCombo); F.add(typeCombo); jTable.revalidate(); jTable.repaint();F.setVisible(true);这段代码为什么不能实现表格筛选
时间: 2023-12-10 08:42:56 浏览: 158
jar.zip解决The import javax.crypto cannot be resolved
5星 · 资源好评率100%
这段代码中,你创建了一个JFrame,添加了一个JTable,并将数据添加到表格中。然后你创建了两个JComboBox,并将它们添加到JFrame中。接着你创建了一个TableRowSorter,并创建了一个compoundRowFilter来过滤表格中的数据。最后,你将两个JComboBox添加了ActionListener,当它们被选择时,会更新compoundRowFilter并将其设置为TableRowSorter的RowFilter。
然而,你没有将TableRowSorter设置为JTable的RowSorter,这是实现表格筛选功能的关键步骤。你需要在创建TableRowSorter之后,添加以下代码:
```
jTable.setRowSorter(tableRowSorter);
```
这将把TableRowSorter作为JTable的RowSorter,并启用表格筛选功能。
阅读全文