0

I have created a class which extends JFrame

public class MyFrame extends JFrame
{
     public MyFrame()
     {
          JPanel panel = new JPanel();
               JPanel panel2 = new JPanel();
                    JDialog myDialog = new JDialog(MyFrame, Dialog.ModalityType.DOCUMENT_MODAL);                        
                    //How can I set my MyFrame as Parent for JDialog???
               panel.add(panel2);
          add(panel);
     }
}

In above code I want to set MyFrame as JDialog's parent. How can I do so? I tried putting like as I have shown in my code but that gives me error.

How can I set my MyFrame as Parent for JDialog???

1 Answers1

1

You use the this keyword.

JDialog myDialog = new JDialog(this, Dialog.ModalityType.DOCUMENT_MODAL);    

More info here: http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

Side note: You shouldn't extend JFrame for no reason. You should favor composition over inheritence. More info here and here.

Community
  • 1
  • 1
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • putting **this** works but would you like to tell me why **this** refers to MyFrame not JPanel while this JDialog is created inside a JPanel? –  Oct 01 '14 at 18:13
  • @ColdCoder Please read the links I provided. The this keyword refers to the enclosing instance, which is the instance of MyFrame. – Kevin Workman Oct 01 '14 at 18:15
  • Thanks for your kind answer and specially thanks for links. Those are really helpful. Thanks –  Oct 01 '14 at 18:20