我使用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. 在二叉搜索樹中搜索值