0

I am trying to update a JTable from another JFrame without closing the main JFrame. The main JFrame has the JTable, the second JFrame has the text fields and the "update" button that get the data to update the JTable in the main window, everything works fine, but if I want the JTable to update correctly, I have to set the visibility of the main frame to "false" and change it to "true" when the "update" button completes the action, so, basically the main window its closed and after the update action is re-open. I want to know if there is any way to do the update without closing the main JFrame.

Button in the main frame that opens the second frame:

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    this.setVisible(false);
    
    UpdtFrame frame = new UpdtFrame();
    
    frame.setVisible(true);
    
}

The button to update in the second JFrame:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    
        Object[] info = {jTextField1.getText(), Double.parseDouble(jTextField2.getText()), Integer.parseInt(jTextField3.getText())}; //get the info from the text fields
        
        updtProdList(jTextField1.getText(), info);
        
        inter.setVisible(true); //object of the main JFrame, opens the frame after do the 
                                //update
        
        this.dispose();
}

The update operation:

public void updtProdList(String name, Object[] info)
{
    for(int i = 0; i < inter.jTable1.getRowCount(); i++)
        {
            if(inter.jTable1.getValueAt(i, 0).toString().equalsIgnoreCase(name))
            {
                for(int j = 0; j < info.length; j++)
                {
                    inter.jTable1.setValueAt(info[j], i, j);
                }
                
                break;
            }
        }
        
        inter.jTable1.revalidate(); //update the table on the main frame
}

I don't know how to do the JTable update without closing the frame and opening it again, any help is appreciated.

Daniel
  • 147
  • 1
  • 7
  • 19
  • 1
    1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) It seems the 2nd frame should be a `JDialog` or a `JOptionPane`. Actually now I see more of the logic, perhaps a `CardLayout`.. 2) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Aug 05 '21 at 05:40
  • @AndrewThompson your post about multiple JFrame(s) is a little bit outdated. Now most of users (at least in corporate sector) have multiple screens. So it would be wrong to ignore this trend and don't adopt applications to work in multiscreen environment. – Sergiy Medvynskyy Aug 05 '21 at 06:16
  • @SergiyMedvynskyy I stick by my answer for the moment. Of course the question is closed so you cannot enter your own, but you might upvote or apply a bounty to [this answer](https://stackoverflow.com/a/17961122/418556) which gives a different take on it. – Andrew Thompson Aug 05 '21 at 07:23
  • No idea why you need to play with visibility of components. Your application should have a single main JFrame that displays the table. If you want a "popup window" where you can edit the data you use a JDialog. Then you get the data entered in the text field and update the TableModel of the JTable. So you can easily pass the TableModel to the "popup Windowj". There is no need for a revalidate() statement. When you update the TableModel the table is repainted automatically. – camickr Aug 05 '21 at 13:35
  • @SergiyMedvynskyy I agree with Andrew. He didn't suggest you can't have multiple screens. The suggestion is the child window should likely be a modal JDialog. That certainly appears to be the case with this question. If I understand this question the application has a main frame with a JTable. To change data in the table a child window is displayed. This window should be a modal JDialog NOT a JFrame. – camickr Aug 05 '21 at 13:38
  • Also consider a _modeless_ dialog, mentioned [here](https://stackoverflow.com/a/18524507/230513) and [here](https://stackoverflow.com/a/11832979/230513). Modality aside, updating the table's model should cause the table to update itself. As you are using a GUI editor, also consider the approach suggested [here](https://stackoverflow.com/a/2561540/230513). – trashgod Aug 05 '21 at 17:15

1 Answers1

0

I have been reading all the material provided in the comments, thanks to that I was able to find a solution using JDialog.

So, in the class of the created JDialog, it is important to set the main JFrame instance as the "parent" of the JDialog:

public class UpdtDialog extends javax.swing.JDialog {
    MainJFrame  frame = new MainJFrame();

    public UpdtDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocation(50, 300);
        setSize(400, 300);
        frame = (MainJFrame) parent;
    }

    //then all the code necessary, in my case it will be button and the 
    //update method
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        //TODO
    }

    public void updtProdList(String name, Object[] info){
        //TODO
    }
}

On the main frame the button that shows the JDialog will have the instance of the JDialog class.

public class InterfazSwing extends javax.swing.JFrame {

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
         UpdtDialog diag = new UpdtDialog(this, true);
         diag.setVisible(true);
    }

}

That way, I was able to solve the problem I was facing while updating the JTable.

Daniel
  • 147
  • 1
  • 7
  • 19