2016-03-06 32 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); 
      } 
     }); 
    } 

}