关闭JDialog会留下一个空框架

时间:2016-06-11 21:38:19

标签: java swing jdialog

我对JDialog有一个小问题,尽管我所做的一切都在关闭后留下一个空框架。不幸的是,我一直在努力寻找解决方案,而不是这个

daughterWindow.dispatchEvent(new WindowEvent(validation, WindowEvent.WINDOW_CLOSING));

,也不

daughterWindow.setVisible(false);
daughterWindow.dispose();

也没有这个帮助过我

WindowAdapter adapter = (WindowAdapter)jdialog.getWindowListeners()[0];
adapter.windowClosing(new WindowEvent((Window)jdialog, WindowEvent.WINDOW_CLOSING));

最有可能的原因是,最后一个抛出了ClassCastException。

  

线程“AWT-EventQueue-0”中的异常java.lang.ClassCastException:javax.swing.SwingUtilities $ SharedOwnerFrame无法强制转换为java.awt.event.WindowAdapter

这是我的代码,也许有人可以给我一个提示。

    JDialog daughterWindow = new JDialog();
    daughterWindow.setModal(true);
    daughterWindow.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    daughterWindow.getContentPane().setLayout(new BoxLayout(daughterWindow.getContentPane(), BoxLayout.Y_AXIS));

    UIManager.put("FileChooser.readOnly", Boolean.TRUE);
    JFileChooser open = new JFileChooser();
    File rsc = new File(System.getProperty("user.dir") + "\\rsc\\");
    if(!rsc.exists()) rsc.mkdir();
    open.setCurrentDirectory(new File(System.getProperty("user.dir") + "\\rsc\\"));
    open.setDialogTitle("Ordner mit der Datenbank auswählen");
    open.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

    if(open.showOpenDialog(daughterWindow) == JFileChooser.APPROVE_OPTION){
        UIManager.put("FileChooser.readOnly", Boolean.FALSE);
        setValidateAccessWindowLayout(open.getSelectedFile());
        daughterWindow.dispatchEvent(new WindowEvent(daughterWindow, WindowEvent.WINDOW_CLOSING));
    } else{
        UIManager.put("FileChooser.readOnly", Boolean.FALSE);
        daughterWindow.dispatchEvent(new WindowEvent(daughterWindow, WindowEvent.WINDOW_CLOSING));
    }

    daughterWindow.setResizable(false);
    daughterWindow.pack();
    daughterWindow.setVisible(true);

And a screen of the problem
提前谢谢!

更新: 检查了第三个选项而没有明显不必要的演员,但这也没有帮助。

WindowListener adapter = daughterWindow.getWindowListeners()[0];
adapter.windowClosing(new WindowEvent(daughterWindow, WindowEvent.WINDOW_CLOSING));

1 个答案:

答案 0 :(得分:0)

您在提供的代码片段中创建了两个可视元素:

JDialog daughterWindow = new JDialog();

JFileChooser open = new JFileChooser();

它们都被定义为模态。这基本上意味着 - 当一个人活跃时,其他人不活跃,甚至没有可能获得控制权。

我建议您设置daughterWindow.setModal(false);

并移动

open.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

daughterWindow.setResizable(false);
daughterWindow.pack();
daughterWindow.setVisible(true);         

if(open.showOpenDialog(daughterWindow) == JFileChooser.APPROVE_OPTION){

更新。从代码中可以看出文件对话框是模态的,但根据Java Doc,它是:

File choosers provide a GUI for navigating the file system, and then either choosing a file or directory from a list, or entering the name of a file or directory. To display a file chooser, you usually use the JFileChooser API to show a modal dialog containing the file chooser. https://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html