Search code examples
javaswingnetbeansjframedefaulttablemodel

Updating JTable from a different JFrame


Actually, I have a JFrame(the main window) with a JTable in it. And couple of buttons, like Add,Delete,Refresh.

Refresh uses the function(updateTable) that has the following code below and works fine:

 try
        {
            ResultSet R = Home.getStatement().executeQuery("Select * from Schooldata");
            int count =0;
            while(R.next()) {  count++; }



school_data = new String[count][6];
            R = Home.getStatement().executeQuery("Select Schoolname,city,ProgramOpted,coordinator_name,Trainers,contactnum from Schooldata");
            count =0;
            while(R.next())
            {   
                for(int i=0;i<6;i++)
                {   school_data[count][i]= R.getString(i+1);
                    System.out.println(R.getString(i+1));
                }
                count++;
            } 
        }
        catch(SQLException S)   {JOptionPane.showMessageDialog(null,S);}

         jTable1.setModel(new DefaultTableModel(school_data,new String [] {
                "School Name", "City", "Program", "Coordinator", "Trainers", "Contact Number"
            }));

When I click on "Add, another JFrame window appears and asks for Details that is further saved to Database and shows a confirmation message and refreshes the table(on a different JFrame i.e the main Window) using above function.

The Issue is, I'm calling the same function but from an another JFrame.Expecting the changes to be reflected in the main JFrame.

Using the method,new Main().updateTable(); in the below code.It's just not working.

try
            {
                int confirm = Home.getStatement().executeUpdate(Query);
                if(confirm == 1)
                {
                    JOptionPane.showMessageDialog(null,"Record Added","Saved",1);
                    new Main().updateTable();
                }
                else  JOptionPane.showMessageDialog(null,"There is some Error.....","Error",0);


            }
            catch(SQLException S)
            {
                JOptionPane.showMessageDialog(null,S,"Error",0);
            }

Solution

  • Your problem I believe is here (it's hard to tell without a minimal example program:

    int confirm = Home.getStatement().executeUpdate(Query);
    if(confirm == 1)
    {
        JOptionPane.showMessageDialog(null,"Record Added","Saved",1);
        new Main().updateTable();  // ****** HERE ******
    }
    

    You're creating a completely new Main object, changing its state, and hoping that this will change the state of the visualized Main object, but you're finding out that no, it won't.

    Instead you need to pass a reference of the visualized Main object into the code above, and then call methods on it, not on a newly created completely unique object.

    Whatever you do, don't try solving this by making fields or methods static. Yes, that might work, but it breaks OOPs and makes your program difficult to test or extend.


    As an aside, that second dependent window should not be another JFrame, and in fact likely should be a modal JDialog. For if you use a JDialog, then there would be no need for the dialog code to push information into the calling window. Rather the calling code will know when you're done dealing with the dialog, and so at this point if the dialog's state is good (if you didn't say cancel it with no changes), then the calling code can easily pull information from the dialog code. For a concrete example of what I"m talking about, please look at my answer to a similar question about passing information between two windows here.

    Also a similar problem and solution can be found here.