Showing JDialog in taskbar not working

坚强是说给别人听的谎言 提交于 2019-12-01 16:11:55

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/19116310/showing-jdialog-in-taskbar-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!