6

I'm using the below code to showing JDialog on taskbar and is perfectly working in JDK 1.6.

public class test8 {   
    public static void main(String[] args) {   
        Runnable r = new Runnable() {   
            public void run() {
                JDialog d = new JDialog((Frame)null,Dialog.ModalityType.TOOLKIT_MODAL);   
                d.setTitle("title");  
                d.setSize(300,200);  
                d.setVisible(true);  
                System.exit(0);   
            }
        };
        EventQueue.invokeLater(r);   
    }  
}   

But When I'm setting the modality type using the method it's not working

public class test8 {   
    public static void main(String[] args) {   
        Runnable r = new Runnable() {   
            public void run() {
                JDialog d = new JDialog();   
                d.setTitle("title");  
                d.setSize(300,200); 
                d.setModalityType(Dialog.ModalityType.TOOLKIT_MODAL); 
                d.setVisible(true);  
                System.exit(0);   
            }  
        };   
        EventQueue.invokeLater(r);   
    }  
}   

What is the difference betwwen the two codes ? Is there any way to solve this using the method ?

kiheru
  • 6,588
  • 25
  • 31
Nikhil
  • 2,857
  • 9
  • 34
  • 58

1 Answers1

6

The problem is that certain constructors of JDialog create a dummy frame owner if the owner is null for historical reasons. But a Dialog must not have an owner to be visible like a top-level window. I.e.

JDialog d=new JDialog((Window)null);
d.setModalityType(ModalityType.TOOLKIT_MODAL);
d.setVisible(true);

will work.

Holger
  • 285,553
  • 42
  • 434
  • 765