简体   繁体   English

使用 JTable 和 JButton 搜索 ArrayList

[英]Searching ArrayList using JTable and JButton

I have an ArrayList holding football matches and when the user types a date and presses "search" button, a new JTable opens showing you all matches played on that day.我有一个保存足球比赛的 ArrayList,当用户输入日期并按下“搜索”按钮时,会打开一个新的 JTable,显示当天进行的所有比赛。 I have looped to get the date and compared it to the input inside the JTextField but it just gives me an empty table even if there is a record of a match played on the date the user enters.我已经循环获取日期并将其与 JTextField 中的输入进行比较,但它只会给我一个空表,即使在用户输入的日期有比赛记录。 In this code below, I am just using hitting enter on JTextField to execute search because I do not know how to map JTextField to JButton.在下面的这段代码中,我只是使用 JTextField 上的 Enter 键来执行搜索,因为我不知道如何将 JTextField 映射到 JButton。 I have tried but it just prints the search Jbutton name.我试过,但它只打印搜索 Jbutton 名称。

 public void searchMatch(ArrayList<Matches> searchMatch, String e)
{
    DefaultTableModel searchModel = new DefaultTableModel();
    for(int i = 0; i < searchMatch.size(); i++)
    {
        if(searchMatch.get(i).getM_date().equals(e))
        {
            System.out.println(searchMatch.get(i).getM_date());
            String date = searchMatch.get(i).getM_date();
            String teamName = searchMatch.get(i).getM_teamName();
            String teamName2 = searchMatch.get(i).getM_teamName2();
            int goalsScoredTeam1 = searchMatch.get(i).getGoalsTeam1();
            int goalsScoredTeam2 = searchMatch.get(i).getGoalsTeam2();

            Object[] row = {teamName, teamName2, goalsScoredTeam1, goalsScoredTeam2,date};
            searchModel.addRow(row);

            JTable searchTable = new JTable(searchModel);
            searchTable.setFillsViewportHeight(true);
            JPanel searchPanel = new JPanel();
            JScrollPane scrollPane = new JScrollPane(searchTable);
            searchPanel.add(scrollPane);

            JFrame frame = new JFrame("Searched Matches");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            searchTable.setOpaque(true);
            frame.setContentPane(searchPanel);

            frame.pack();
            frame.setSize(500, 500);
            frame.setVisible(true);
        }
    }

}
DefaultTableModel searchModel = new DefaultTableModel();

You TableModel has no columns to display.您的 TableModel 没有要显示的列。

Even though you add rows of data, none of the data can be displayed unless you also have defined the "column names" for the TableModel.即使您添加了数据行,也无法显示任何数据,除非您还为 TableModel 定义了“列名”。

Your code should be something like:你的代码应该是这样的:

String columnNames = { "Date", "Name", "..." };
DefaultTableModel searchModel = new DefaultTableModel(columnNames, 0);

Which will create an empty TableModel with just the column names.这将创建一个只有列名的空 TableModel。 Your looping code will then add each row of data.然后您的循环代码将添加每一行数据。

Note, you should also look at storing all the data in your TableModel and then just filter the TableModel.请注意,您还应该考虑将所有数据存储在 TableModel 中,然后只过滤 TableModel。 Read the section from the Swing tutorial on Sorting and Filtering for a working example.阅读 Swing 教程中关于排序和过滤的部分,以获取工作示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM