public void actionPerformed(ActionEvent e) { if (comboBox.getSelectedItem().equals("书名")) { Book book=new Book(); book.setBookName(textField.getText()); BFormService bs=new BFormService(); List<Book> aList=bs.select_by_id(book); Vector data =new Vector(); Iterator<Book> iterator=aList.iterator(); while(iterator.hasNext()){ Book next = iterator.next(); //用于计算stock-borrow String s=String.valueOf(next.getSum()-next.getBorrow()); // Vector row=new Vector(); row.add(next.getISBN()); row.add(next.getBookName()); row.add(next.getPublisher()); row.add(next.getAuthor()); row.add(s); row.add(next.getPrice()); data.add(row); System.out.println(data); }
时间: 2024-04-01 21:31:57 浏览: 89
这是一个 actionPerformed 方法,它是在用户执行某个动作时被触发的,比如点击按钮或选择下拉列表。在这个方法中,首先判断用户选择的下拉列表项是否为“书名”,如果是,则创建一个 Book 对象,并将输入的书名设置为 Book 对象的书名属性。然后创建一个 BFormService 对象,调用其 select_by_id 方法来获取满足条件的 Book 对象列表。接着,创建一个 Vector 对象 data,用于存储表格中的数据,并通过迭代器遍历 Book 对象列表,将每个 Book 对象的信息加入到 data 中。最后打印出 data,可以看到每个 Book 对象的信息都已经被添加到了 data 中。
相关问题
final String[] categoryFilter = {(String) combobox_category.getSelectedItem()}; final String[] typeFilter = {(String) comboBox_type.getSelectedItem()}; TableRowSorter<TableModel> tableRowSorter = new TableRowSorter<>(table); table1.setRowSorter(tableRowSorter); 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)); } }; combobox_category.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { categoryFilter[0] = (String) combobox_category.getSelectedItem(); tableRowSorter.setRowFilter(compoundRowFilter); } }); comboBox_type.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { typeFilter[0] = (String) comboBox_type.getSelectedItem(); tableRowSorter.setRowFilter(compoundRowFilter); } });这段代码为什么不能混合筛选表格
这段代码应该是用于实现对表格的筛选功能,其中包括两个下拉列表框,分别用于选择表格中的类别和类型。根据用户选择的不同,使用TableRowSorter和RowFilter将符合条件的行筛选出来。
根据代码,可能会出现混合筛选表格的问题在于条件判断的括号没有放置正确,导致在进行类型筛选时,类别的条件判断被忽略了。正确的代码应该是:
```
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));
}
};
```
注意到在判断类别和类型时,每个判断都应该放在括号内,以确保逻辑的正确性。
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);这段代码为什么不能实现表格筛选
这段代码中,你创建了一个JFrame,添加了一个JTable,并将数据添加到表格中。然后你创建了两个JComboBox,并将它们添加到JFrame中。接着你创建了一个TableRowSorter,并创建了一个compoundRowFilter来过滤表格中的数据。最后,你将两个JComboBox添加了ActionListener,当它们被选择时,会更新compoundRowFilter并将其设置为TableRowSorter的RowFilter。
然而,你没有将TableRowSorter设置为JTable的RowSorter,这是实现表格筛选功能的关键步骤。你需要在创建TableRowSorter之后,添加以下代码:
```
jTable.setRowSorter(tableRowSorter);
```
这将把TableRowSorter作为JTable的RowSorter,并启用表格筛选功能。
阅读全文