2016-03-06 33 views
1

我正在構建一個java程序,其中您應該能夠選擇一個文件。對話框應該彈出爲JInternalFrame,並且不應該是自己的窗口。我的進步:JFileChooser as JInternalFrame

JFileChooser chooser = new JFileChooser(); 
      JInternalFrame fr = new JInternalFrame("Öffnen", true, // resizable 
        false, // closable 
        true, // maximizable 
        true);// iconifiable); 
      fr.add(chooser); 
      fr.setSize(300, 600); 
      fr.setVisible(true); 
      JOS.mainWindow.jdpDesktop.add(fr); 
      chooser.setVisible(true); 
      chooser.setSize(300, 600); 

      chooser.addActionListener(new ActionListener() { 

       @Override 
       public void actionPerformed(ActionEvent e) { 
        fr.setVisible(false); 
        JOS.mainWindow.jdpDesktop.remove(fr); 
       } 
      }); 

它關閉,如果我按下關閉按鈕,但我沒有得到一個事件如果在用戶按下「打開」。有沒有我可以使用的ActionListener?還有其他方法可以做到嗎? 謝謝! -Jakob

+0

'JFileChooser'是一個組件,它有一個方便的方法,它允許你顯示一個對話框。你可以使用像'JOptionPane.showInternalOptionDialog'這樣的東西,並通過它呈現'JFileChooser'的一個實例 – MadProgrammer

回答

3

JFileChooser只是一個組成部分,它有一個方便的方法,可以讓你內表現出來一個對話框

你可以使用JOptionPane.showInternalOptionDialog展現JFileChooser,它會像一個模態對話框,但包裹在JInternalFrame例如...

JInternaFrame JFileChooser

import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JDesktopPane; 
import javax.swing.JFileChooser; 
import javax.swing.JFrame; 
import javax.swing.JInternalFrame; 
import javax.swing.JOptionPane; 
import javax.swing.SwingUtilities; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Test { 

    public static void main(String[] args) { 
     new Test(); 
    } 

    public Test() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JDesktopPane dp = new JDesktopPane(); 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(dp); 
       frame.setSize(800, 800); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 

       JFileChooser chooser = new JFileChooser(); 
       chooser.addActionListener(new ActionListener() { 
        @Override 
        public void actionPerformed(ActionEvent e) { 
         JInternalFrame parent = (JInternalFrame) SwingUtilities.getAncestorOfClass(JInternalFrame.class, chooser); 
         if (JFileChooser.CANCEL_SELECTION.equals(e.getActionCommand())) { 
          // Dialog was canceled 
         } else if (JFileChooser.APPROVE_SELECTION.equals(e.getActionCommand())) { 
          // Dialog was "approved" 
         } 
         parent.dispose(); 
        } 
       }); 
       JOptionPane.showInternalOptionDialog(dp, chooser, "Choose", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, new Object[0], null); 
      } 
     }); 
    } 

}