简体   繁体   中英

Calling a Jframe object from a closing JDialog object

I am creating a basic Swing application. In which I have a JFrame (which has a JTable ). This window has add record button, where I open a JDialog window which has few data field. Once the use enter the value for those fields. I need to add those information as a record in the JTable with in the JFrame .

To do this, I open the JFrame window. Once the user press 'Add' button. The JDialogbox window opens (While the JFrame is still open in the background). Then user enters some information and it needs to be pass back to the JFrame .

How can I do this without having to create a new JFrame object. Is there any way to use the parent element in the JDialog box constructor?

(javax.swing.JFrame parent, boolean modal)

Here is a skeleton of code that shows a way to achieve what you want using a callback method on your frame.

public class DataBean{
     // keep all variables that needs to be transferred
}

public class MyFrame extends JFrame {
      // Frame code here

     public void openDialog() {
          new MyDialog(this,true).setVisible(true);
     }

    public void addRowToTable(DataBean data) {
       // add row to table from data 
    }
}

public class MyDialog extends JDialog {
    // Dialog display code

    DataBean data = new DataBean();
    // populate DataBean object from dialog fields
    // dispose dialog
    // call method to pass
    ((MyFrame)getOwner()).addRowToTable(data);
}

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM