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

  

  

  

Blue Captcha Image
Refresh

*

September 2019
M T W T F S S
« Aug    
 1
2345678
9101112131415
16171819202122
23242526272829
30