Displays a specified JFileChooser in a JInternalFrame. - Java Swing

Java examples for Swing:JFileChooser

Description

Displays a specified JFileChooser in a JInternalFrame.

Demo Code


//package com.java2s;
import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDesktopPane;
import javax.swing.JFileChooser;
import javax.swing.JInternalFrame;

public class Main {
    /**/*from w w w.ja  v  a 2s  . c  o m*/
     * Displays a specified <code>JFileChooser</code> in a JInternalFrame. <br />
     * The JInternalFrame will close when the dialog is closed. <br />
     * 
     * @param desktop the JDesktopPane on which to display the JFileChooser
     * @param ch the JFileChooser to display
     */
    public static void showInternalFileChooser(JDesktopPane desktop,
            final JFileChooser ch) {
        final JInternalFrame frm = new JInternalFrame(ch.getDialogTitle());
        frm.setClosable(true);
        frm.setResizable(true);
        frm.setLayout(new BorderLayout());
        frm.add(ch, BorderLayout.CENTER);
        frm.setVisible(true);

        frm.pack();

        Dimension size = frm.getSize();
        frm.setLocation(desktop.getWidth() / 2 - size.width / 2,
                desktop.getHeight() / 2 - size.height / 2);
        desktop.add(frm, 0);

        ch.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                frm.dispose();
                ch.removeActionListener(this);
            }
        });

        try {
            frm.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {
        }
    }
}

Related Tutorials