【问题标题】:How to implement Search in a JTable [closed]如何在 JTable 中实现搜索 [关闭]
【发布时间】:2012-08-20 20:20:33
【问题描述】:

我希望 JTable 具有一个功能,其中我将提供一个文本字段来输入要从 JTable 中搜索的值,如果输入的值与 JTable 中的任何单元格值匹配,则应突出显示该特定单元格& 单元格字体应变为粗体。当用户在文本字段中指定值后按 Enter 键时,值将被匹配。

我该怎么做?

【问题讨论】:

  • 太棒了!所以what have you tried?你有什么问题?
  • 我什么都没试过,只是想知道有没有简单的方法。
  • 是的,大部分内容在JTable tutorials 中都有很好的解释。你已经通过它们了吗?
  • 您可以在模型中搜索感兴趣的文本至于突出显示和粗体,这是单元格渲染器的工作。
  • 对不起,我没有看到上面的链接,非常感谢。

标签: java swing search jtable


【解决方案1】:

这是解决问题的一种方法。代码如下:

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】:

    来自 SwingX 项目的 JXTable 内置支持搜索表(查找 Searchable 接口)。它还允许快速创建使用此Searchable 接口的搜索字段:JXSearchField 和/或JXSearchPanel。

    如果我没记错的话,这将满足您的大部分要求。可能只需要添加一些自定义代码来使单元格内容变为粗体

    【讨论】:

    • 不确定是谁投了反对票。赞成取消它。这是一个有效的答案
    • @HovercraftFullOfEels 我也想知道投反对票的原因是什么。如果投反对票的人至少在评论中指出投反对票的原因,那总是很好,让我可以更正/改进我的答案
    • +1 我认为这是个好主意
    • 我如何“链接”JXSearchField 和JXTable?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-07
    • 1970-01-01
    • 1970-01-01
    • 2015-08-23
    • 1970-01-01
    相关资源
    最近更新 更多