JTable を使用して、JTable から検索する値を入力するためのテキストフィールドを提供する機能が必要です。この入力された値が JTable のいずれかのセル値と一致する場合は、その特定のセルを強調表示してセルを強調表示する必要があります。フォントを BOLD にする必要があります。ユーザーがテキスト フィールドに値を指定して Enter キーを押すと、値が一致します。
これどうやってするの?
これは問題を解決する方法です。コードは次のとおりです。
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
.
私の記憶が正しければ、これでほとんどの要件をカバーできます。おそらく、セルの内容を太字にするためのカスタム コードを追加するだけで済みます。