user4099884
user4099884

Reputation:

How to set Main frame in child JDialog

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???

Upvotes: 0

Views: 241

Answers (1)

Kevin Workman
Kevin Workman

Reputation: 42176

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.

Upvotes: 1

Related Questions