我想要JTable的一個特性,其中我將提供一個文本字段以輸入要從JTable搜索的值,並且如果此輸入值與JTable中的任何單元格值匹配那麼該特定單元格應該突出顯示&單元格字體應該變爲BOLD。在用戶在文本字段中指定值後按Enter鍵時,這些值將被匹配。如何在JTable中實現搜索
我怎樣才能做到這一點?
我想要JTable的一個特性,其中我將提供一個文本字段以輸入要從JTable搜索的值,並且如果此輸入值與JTable中的任何單元格值匹配那麼該特定單元格應該突出顯示&單元格字體應該變爲BOLD。在用戶在文本字段中指定值後按Enter鍵時,這些值將被匹配。如何在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();
}
});
}
}
來自SwingX項目的JXTable
具有內置支持來搜索表(查找Searchable
接口)。它還允許快速創建一個使用此接口Searchable
的搜索字段:JXSearchField
和/或JXSearchPanel
。
如果我沒記錯的話,這將覆蓋你的大部分要求。可能只需要添加一些用於使單元格內容大膽的自定義代碼
不確定是誰給了反對票。投票決定取消它。這是一個有效的答案 –
@HovercraftFullOfEels我也想知道downvote的原因是什麼。總是很好,如果downvoter至少在評論中指出爲什麼投票,讓我糾正/改善我的回答 – Robin
是。同意100%。 –
太棒了!所以[你有什麼嘗試?](http://mattgemmell.com/2008/12/08/what-have-you-tried/)你有什麼問題? –
我沒有嘗試任何東西,只是我想知道是否有一種簡單的方法來做到這一點。 – aoulhent
是的,大部分內容在[JTable教程](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html)中都有詳細解釋。你有沒有經歷過他們? –