jtable在添加到arraylist时没有更新?

1bqhqjot  于 2021-07-14  发布在  Java
关注(0)|答案(0)|浏览(242)

现在我有一个数组列表

static List<List<String>> lines = new ArrayList<>();

有这样的价值观

{["ID", "Last Name", "First Name", "Vaccine Type", "Vaccination Date", "Vaccine Location"]
["12345", "Doe", "John", "Pfizer", "10/30/2020", "Argentina"]
["54321", "Adam", "Marceline", "Pfizer", "11/19/2020", "Russia"]}

我想在按下按钮时将其显示在gui中的jtable中,这样可以正常工作。

private void initialize(){

    btnLoadData = new JButton("Load Data");
    btnLoadData.addActionListener(this);
    btnLoadData.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {

                    String[] header = {"ID", "Last Name", "First Name", "Vaccine Type", "Vaccination Date", "Vaccine Location" };
                    String[][] tempTable = new String[lines.size()][]; 
                    int i = 0;
                    for (List<String> next : lines) {
                        tempTable[i++] = next.toArray(new String[next.size()]); // return Object[][]
                }
            JTable EndTable = new JTable(tempTable,header);
            EndTable.setBounds(30, 40, 200, 300);
            panel_2.add(new JScrollPane(EndTable));

        }
        });

}
但是,当我尝试向arraylist中添加一个额外的行,然后再次按下按钮时,jtable不会更新(在gui的表的底部)。

btnSubmit_3 = new JButton("Submit");
btnSubmit_3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            String id = textField_1.getText();
            String Lname = textField_2.getText();
            String Fname = textField_3.getText();
            String Vtype = textField_4.getText();
            String Vdate = textField_5.getText();
            String Loc = textField_6.getText();
            List<String> new_row = Arrays.asList(id, Lname, Fname, Vtype, Vdate, Loc);
            lines.add(new_row);
            }
            textField_1.setText("");
            textField_2.setText("");
            textField_3.setText("");
            textField_4.setText("");
            textField_5.setText("");
            textField_6.setText("");

        }
    });
    panel_3.add(btnSubmit_3);

我知道它确实会添加到数组列表中,因为我打印了数组列表,并且那里有一个包含用户值的额外行。但是,每当我再次按下按钮时,jtable似乎就在gui中一成不变了。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题