在 JTable 中搜索

发布于 2024-09-09 21:04:13 字数 81 浏览 3 评论 0原文

我使用 Java Swing 创建了一个对话框窗口,该窗口在 JTable 中显示项目列表。我想实现某种搜索功能。谁能建议我实现此类功能的最佳方法?

I have created with Java Swing a dialog window which presents in a JTable a list of items. I would like to implement some sort of search functions. Can anyone suggest me the best way to implement such feature?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拥醉 2024-09-16 21:04:14

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

Read the JTable API and follow the link to the Swing tutorial on "How to Use Tables". There you will find a section on "Sorting and Filtering" which gives an example of how to use a text field to search for rows containing the specified text.

昔梦 2024-09-16 21:04:14

这是在 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();
    }
});
  }
 }

This is a way to implement search in a 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();
    }
});
  }
 }
给不了的爱 2024-09-16 21:04:14

看一下 SwingX 及其 JXTable。 SwingX 提供了一组扩展普通 Swing 组件并向其添加额外功能的组件。我最喜欢的是 MultiSplitPane (它是 JSplitPane 的替代品,允许您将窗格划分为任意数量的可调整大小的部分)和 JXTable ,它就像 JTable< /code>,而且还具有内置搜索功能(并绑定到 Ctrl-F),您可以对行进行排序/过滤。非常整洁的东西。您需要做的就是导入库,将 JTable 更改为 JXTable (以相同的方式启动),瞧!

希望有帮助。

Take a look at SwingX, and its JXTable. SwingX provides a set of components that extend normal Swing components and adds extra functionality to them. My favorites are MultiSplitPane (which is an alternative to JSplitPane and allows you to divide panes in any number of resizable sections) and JXTable which is just like a JTable, but also has search functionality built-in (and bound to Ctrl-F) and you can sort/filter the rows. Pretty neat stuff. All you need to do is import the library, change your JTable to JXTable (initiated the same way), and voila!

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文