JTable isn't updating when adding to Arraylist?

69 views Asked by At

Ok so I currently have a array list constructed as

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

with values like this

    {["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"]}

I want to display this into a JTable in a GUI when a button is pressed, which works fine.

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));
            
        }
        });

}

However when I try to add an extra row into the Arraylist and then press the button again, the JTable is not updated(at the bottom of the Table in the 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);

I know it does add to the array list because I printed the array list out and there is an extra row there with the user values. But it seems that the JTable is set in stone in the GUI whenever I try to press the button again.

0

There are 0 answers