1
votes

I want to add row in a JTable consistently. I have two Frames. From the first frame I will take data like below-

myFrame.java (Frame 1)

And in the second frame I have made a JTable like-

List.java (Frame 2)

I have added an actionListener in the first frame's "Submit" button like-

private void submitActionPerformed(java.awt.event.ActionEvent evt) {                                       

    int a = 1, room = 301;
    List li = new List();

    DefaultTableModel model = (DefaultTableModel) List.table.getModel();
    model.addRow(new Object[]{a++,name.getText(),mobile.getText(),ward.getText(),room++});

    li.setVisible(true);

}

I could add a row successfully. But, the problem is when I add another Patient detail, It adds that data in a different frame and previous data vanishes itself. I don't know the cause as I doing this for the first time. I want to show this in list frame consistently.

I have tried to do this in the same frame, if I input data and click OK, It can add the row one by one, I searched in youtube and found this to do this in only one frame, when I want to do so in two separate frame, it don't add rows. It repaces the previous row only.

1
Pass a reference of the TableModel to the second class, fill as desired; Use an Observer Pattern to get notifications from the "generator" class for when things have been "generated", fill table as desired; Use a Producer/Consumer Pattern, fill as desired; Use a dialog, fill in the details, when closed, as the dialog for the details and fill table as desired, see How to Make Dialogs for more detailsMadProgrammer
As I am new to this, will you please explain more? Or, can you give a link to learn this process?Utpal Barman
There are three links in the previous comment, I recommend the last oneMadProgrammer
Is there any way to store previous data that I added to table even if I close the frame window?Utpal Barman
For a discussions on your options, have a look at thisMadProgrammer

1 Answers

0
votes

Simply because you created the JFrame object "Li" inside the ActionListener function so each time you click the button it creates a new frame, try creating the JFrame Object outside the function it should work