Search code examples
javaswingjava-8jframejtable

How to refresh JTable of a JFrame from a another JFrame without "closing" the first JFrame?


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.


Solution

  • 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.