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

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

现在我有一个数组列表

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

有这样的价值观

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

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

  1. private void initialize(){
  2. btnLoadData = new JButton("Load Data");
  3. btnLoadData.addActionListener(this);
  4. btnLoadData.addActionListener(new ActionListener() {
  5. public void actionPerformed(ActionEvent e) {
  6. String[] header = {"ID", "Last Name", "First Name", "Vaccine Type", "Vaccination Date", "Vaccine Location" };
  7. String[][] tempTable = new String[lines.size()][];
  8. int i = 0;
  9. for (List<String> next : lines) {
  10. tempTable[i++] = next.toArray(new String[next.size()]); // return Object[][]
  11. }
  12. JTable EndTable = new JTable(tempTable,header);
  13. EndTable.setBounds(30, 40, 200, 300);
  14. panel_2.add(new JScrollPane(EndTable));
  15. }
  16. });

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

  1. btnSubmit_3 = new JButton("Submit");
  2. btnSubmit_3.addActionListener(new ActionListener() {
  3. public void actionPerformed(ActionEvent e) {
  4. String id = textField_1.getText();
  5. String Lname = textField_2.getText();
  6. String Fname = textField_3.getText();
  7. String Vtype = textField_4.getText();
  8. String Vdate = textField_5.getText();
  9. String Loc = textField_6.getText();
  10. List<String> new_row = Arrays.asList(id, Lname, Fname, Vtype, Vdate, Loc);
  11. lines.add(new_row);
  12. }
  13. textField_1.setText("");
  14. textField_2.setText("");
  15. textField_3.setText("");
  16. textField_4.setText("");
  17. textField_5.setText("");
  18. textField_6.setText("");
  19. }
  20. });
  21. panel_3.add(btnSubmit_3);

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

暂无答案!

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

相关问题