2016-06-11 46 views
0

我有一個小問題,一個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
預先感謝您!

UPDATE: 簽出無明顯不必要投第三個選項,但沒有很好的幫助。

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

爲了更好地幫助更快,發佈[MCVE]或[短的,獨立的,正確的示例](http://www.sscce.org/)。所有的 –

回答

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文檔,它是:

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

+0

首先感謝您的線索!我試着玩的'daughterWndow'元素的形態和恩和各路禁用它,唉,沒有任何明顯的效果。此外,我嘗試後治療的情況下'如果(open.showOpenDialog(daughterWindow)== JFileChooser.APPROVE_OPTION)'或者也許是毫無意義可言? –

+0

確保您展示'daughterWindow'打開樣式文件選擇對話框之前,而不是之後。 –

+0

就是這樣!在這種情況下,我們不僅應該設置'daughterWindow',否則它會連續出現兩次。非常感謝你! –