2012-12-27 24 views
0
final JFileChooser chooser = new JFileChooser(); 
JOptionPane.showInternalOptionDialog(this, chooser, "Browse", 
     JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, 
     new Object[]{}, null); 
      chooser.addActionListener(new ActionListener() {   
       public void actionPerformed(ActionEvent e) { 
        if(e.getActionCommand().equals(JFileChooser.APPROVE_SELECTION)) 

         System.out.println("File selected: " + 
chooser.getSelectedFile()); 
         //code to close here 
        } else { 
         //code to close here 
        } 
       } 
      }); 

這段代碼看起來很奇怪,但它只是我程序的一部分。我使用全屏GraphicsDevice。我將文件選擇器放在內部JOptionPane中以保留我的全屏窗口。現在我想以編程方式關閉內部本身而不關閉我的actionlistener中的整個應用程序。如何做呢?關閉一個JOptionPane.showInternalOptionDialog窗口本身

+1

1)爲了更好地提供幫助,請發佈[SSCCE](http://sscce.org/)。 2)*「這段代碼看起來很奇怪,......」*是的,它的確如此。忘記「現在是什麼」並回答「爲什麼?」。這個功能的用途是什麼 - 或者提供的功能是什麼? *「但它只是我的程序的一部分。」*不知何故,這並不令人放心。 –

回答

0

當您調用showInternalOptionDialog時,無法輕鬆訪問對話框的引用。你可以添加選項,即。使用

new Object[] { "Browse", "Cancel" } 

,而不是

new Object[]{} 

但隨後你會最終有兩組按鈕。我認爲在JFileChooser中使用showInternalOptionDialog沒有簡單的方法。我會建議你自己創建JInternalFrame。

final JFileChooser chooser = new JFileChooser(); 
JOptionPane pane = new JOptionPane(chooser, JOptionPane.PLAIN_MESSAGE, 
      JOptionPane.DEFAULT_OPTION, null, new Object[0], null); 
final JInternalFrame dialog = pane.createInternalFrame(this, "Dialog Frame"); 
chooser.addActionListener(new ActionListener() { 

    public void actionPerformed(ActionEvent e) { 
     if (e.getActionCommand().equals(JFileChooser.APPROVE_SELECTION)) { 
      System.out.println("File selected: " 
        + chooser.getSelectedFile()); 
     } 
     dialog.setVisible(false); 
    } 
}); 
dialog.setVisible(true); 

也在你的代碼片段中,你調用showInternalOptionDialog後的addActionListener。 showInternalOptionDialog調用只有在關閉對話框後纔會返回,因此ActionListener將會創建得太晚。您需要先添加ActionListener,然後顯示對話框。