How to make a form refer to the parent frame using a JDialog

Below code segment shows how to use a JDialog to refer to a parent frame.
MyApp.java

public class MyApp {
   JDialog dialog;
   JFrame parentFrame;

   public static void main(String[] args) {
      MyApp app = new MyApp();
      app.gui();
   }

   public void gui() {
      //The parentFrame
      parentFrame = new JFrame();
      parentFrame = setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      //The dialog
      dialog = new JDialog(parentFrame, "My New Application v 1.0"):
      dialog.addWindowListener(new CloseWindowListener());

      dialog.setSize(300, 300);
      dialog.setResizable(false);
      dialog.setVisible(true);
   }

   //The close window event handler
   public class CloseWindowListener implements WindowListener {
      public void windowClosing(WindowEvent ev) {
         dialog.dispose();              //First, dispose the dialog
         parentFrame.dispose();         //Then, dispose the frame
      }
   }
}

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s