问题描述:

I have a small problem with a JDialog, it leaves an empty frame after closing despite everything I do. I've been trying quite long to find a solution, unfortunately, neither this

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

nor

daughterWindow.setVisible(false);

daughterWindow.dispose();

nor this one helped me

WindowAdapter adapter = (WindowAdapter)jdialog.getWindowListeners()[0];

adapter.windowClosing(new WindowEvent((Window)jdialog, WindowEvent.WINDOW_CLOSING));

most likely, because of a ClassCastException the last one throws.

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.SwingUtilities$SharedOwnerFrame cannot be cast to java.awt.event.WindowAdapter

Here's my code, maybe someone can give me a hint.

 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

Thank you in advance!

UPDATE:

checked out the third option without apparently needless cast, but that didn't help as well.

WindowListener adapter = daughterWindow.getWindowListeners()[0];

adapter.windowClosing(new WindowEvent(daughterWindow, WindowEvent.WINDOW_CLOSING));

网友答案:

You have two visual elements created in the provided fragment of the code:

JDialog daughterWindow = new JDialog();

and

JFileChooser open = new JFileChooser();

Both of them defined as modal. Which basically means -- while one is active, other is not active, not even has a possibility to gain control.

I suggest you set your daughterWindow.setModal(false);

and move

open.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

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

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

Update. It is not obvious from the code that file dialog is modal, but as per Java Doc it is:

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

相关阅读:
Top