我使用Java Swing创建了一个对话窗口,它在JTable中显示一个项目列表。我想实现某种搜索功能。任何人都可以建议我实现此功能的最佳方式?在JTable中搜索
0
A
回答
1
阅读JTable API,并按照“如何使用表格”Swing教程的链接。在那里你会找到一个关于“排序和过滤”的部分,它举例说明了如何使用文本字段来搜索包含指定文本的行。
1
这是落实JTable中搜索的方式:
public class JTableSearchAndHighlight extends JFrame {
private JTextField searchField;
private JTable table;
private JPanel panel;
private JScrollPane scroll;
public JTableSearchAndHighlight() {
initializeInventory();
}
private void initializeInventory() {
panel = new JPanel();
searchField = new JTextField();
panel.setLayout(null);
final String[] columnNames = {"Name", "Surname", "Age"};
final Object[][] data = {{"Jhon", "Java", "23"}, {"Stupid", "Stupido", "500"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Max", "Dumbass", "10"}, {"Melanie", "Martin", "500"},
{"Jollibe", "Mcdonalds", "15"}};
table = new JTable(data, columnNames);
table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(true);
scroll = new JScrollPane(table);
scroll.setBounds(0, 200, 900, 150);
searchField.setBounds(10, 100, 150, 20);
searchField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String value = searchField.getText();
for (int row = 0; row <= table.getRowCount() - 1; row++) {
for (int col = 0; col <= table.getColumnCount() - 1; col++) {
if (value.equals(table.getValueAt(row, col))) {
// this will automatically set the view of the scroll in the location of the value
table.scrollRectToVisible(table.getCellRect(row, 0, true));
// this will automatically set the focus of the searched/selected row/value
table.setRowSelectionInterval(row, row);
for (int i = 0; i <= table.getColumnCount() - 1; i++) {
table.getColumnModel().getColumn(i).setCellRenderer(new HighlightRenderer());
}
}
}
}
}
});
panel.add(searchField);
panel.add(scroll);
getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Inventory Window");
setSize(900, 400);
setLocationRelativeTo(null);
setVisible(true);
}
private class HighlightRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
// everything as usual
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
// added behavior
if(row == table.getSelectedRow()) {
// this will customize that kind of border that will be use to highlight a row
setBorder(BorderFactory.createMatteBorder(2, 1, 2, 1, Color.BLACK));
}
return this;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new JTableSearchAndHighlight();
}
});
}
}
0
相关问题
- 1. 在jTable中添加搜索方法(jQuery)
- 2. 在jtable netbeans中过滤和搜索
- 3. 如何在JTable中实现搜索
- 4. 实现搜索textField到jTable
- 5. 搜索结果列表JTable
- 6. 如何搜索JTable java中的元素?
- 7. JTable中显示JTextFiled搜索结果
- 8. 显示jquery中的搜索jtable的数据jtable
- 9. 在日期之间进行搜索如何在jtable中显示
- 10. Swing JTable定制过滤/搜索
- 11. 如何重置Jtable,搜索后
- 12. 使用JTextfield文本重复搜索JTable
- 13. 在jtable中检索图像
- 14. 如何在jtable中显示搜索结果?
- 15. 如何在JTable中执行搜索并显示结果
- 16. 如何在jTable中编辑和搜索数据(单词)?
- 17. 搜索查询并在jtable中显示结果
- 18. 在JTable中搜索 - 没有得到正确的输出
- 19. 如何在Jtable中搜索特定数据?
- 20. JTable在JFrame中仅显示一次用于搜索查询
- 21. 在JTable中搜索通过JTextField数据输入的数据
- 22. 搜索在搜索框中
- 23. 在搜索后在Jtable上显示数据
- 24. 搜索在搜索
- 25. 如何搜索SQL数据库并在JTable上显示
- 26. 搜索:搜索()vs cts:搜索()在MarkLogic
- 27. 在BestBuy中搜索由UPC搜索
- 28. 搜索问号(?)在Azure中搜索
- 29. 在搜索栏中实现Google搜索
- 30. 在二叉搜索树中搜索值