2

I'm building a java program, in which you should be able to select a file. The Dialog should popup as JInternalFrame and shouldn´t be an own window. My progress:

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);
                }
            });

It closes if I press the close button, but I dont get an Event if the user presses on "open". Is there any ActionListener I can use? How else to do this? Thanks! -Jakob

Jakob
  • 53
  • 4
  • `JFileChooser` is a component, which has a convenience method which allows you to show a dialog. You could use something like `JOptionPane.showInternalOptionDialog` and present an instance of `JFileChooser` through it – MadProgrammer Mar 06 '16 at 01:40

1 Answers1

4

JFileChooser is just a component, which has a convenience method that allows you to show it within a dialog

You could use JOptionPane.showInternalOptionDialog to show the JFileChooser, it will act like a modal dialog, but wrapped in a JInternalFrame for example...

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);
            }
        });
    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366