在JTable中搜索

时间:2010-07-14 10:12:15

标签: java search jtable

我使用Java Swing创建了一个对话窗口,该窗口在JTable中显示了一个项目列表。我想实现某种搜索功能。任何人都可以建议我实现这样的功能的最佳方式吗?

3 个答案:

答案 0 :(得分:1)

阅读JTable API,并点击“如何使用表格”的Swing教程链接。在那里,您将找到有关“排序和过滤”的部分,其中提供了如何使用文本字段搜索包含指定文本的行的示例。

答案 1 :(得分: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();
    }
});
  }
 }

答案 2 :(得分:0)

查看SwingX及其JXTable。 SwingX提供了一组扩展普通Swing组件的组件,并为它们增加了额外的功能。我最喜欢的是MultiSplitPane(它是JSplitPane的替代品,允许您在任意数量的可调整大小的部分中划分窗格)和JXTable,就像JTable一样,但也具有搜索功能内置(并绑定到Ctrl-F),您可以排序/筛选行。很整洁的东西。您需要做的就是导入库,将JTable更改为JXTable(以同样的方式启动),然后瞧!

希望有所帮助。