Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing JDialog in taskbar not working

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 ?

like image 731
Nikhil Avatar asked Oct 01 '13 12:10

Nikhil


1 Answers

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.

like image 189
Holger Avatar answered Sep 20 '22 22:09

Holger