Java JButton Settings showJFileChooser(JFileChooser chooser, Component parent, String approveButtonText)

Here you can find the source of showJFileChooser(JFileChooser chooser, Component parent, String approveButtonText)

Description

Displays the given file chooser.

License

Open Source License

Parameter

Parameter Description
chooser the file chooser to display.
parent the parent window.
approveButtonText the text for the approve button.

Return

the return code of the chooser.

Declaration

public static final int showJFileChooser(JFileChooser chooser, Component parent, String approveButtonText) 

Method Source Code


//package com.java2s;
/*//from  www .ja  va2s . c  om
 * Copyright (c) 2001-2002, Marco Hunsicker. All rights reserved.
 *
 * This software is distributable under the BSD license. See the terms of the
 * BSD license in the documentation provided with this software.
 */

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Frame;

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

import javax.swing.JDialog;
import javax.swing.JFileChooser;

public class Main {
    /**
     * Displays the given file chooser. Utility method for avoiding of memory leak in JDK
     * 1.3 {@link javax.swing.JFileChooser#showDialog}.
     *
     * @param chooser the file chooser to display.
     * @param parent the parent window.
     * @param approveButtonText the text for the approve button.
     *
     * @return the return code of the chooser.
     */
    public static final int showJFileChooser(JFileChooser chooser, Component parent, String approveButtonText) {
        if (approveButtonText != null) {
            chooser.setApproveButtonText(approveButtonText);
            chooser.setDialogType(javax.swing.JFileChooser.CUSTOM_DIALOG);
        }

        Frame frame = (parent instanceof Frame) ? (Frame) parent
                : (Frame) javax.swing.SwingUtilities.getAncestorOfClass(java.awt.Frame.class, parent);
        String title = chooser.getDialogTitle();

        if (title == null) {
            title = chooser.getUI().getDialogTitle(chooser);
        }

        final JDialog dialog = new JDialog(frame, title, true);
        dialog.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        Container contentPane = dialog.getContentPane();
        contentPane.setLayout(new BorderLayout());
        contentPane.add(chooser, BorderLayout.CENTER);
        dialog.pack();
        dialog.setLocationRelativeTo(parent);
        chooser.rescanCurrentDirectory();

        final int[] retValue = new int[] { javax.swing.JFileChooser.CANCEL_OPTION };

        ActionListener l = new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                if (ev.getActionCommand() == JFileChooser.APPROVE_SELECTION) {
                    retValue[0] = JFileChooser.APPROVE_OPTION;
                }

                dialog.setVisible(false);
                dialog.dispose();
            }
        };

        chooser.addActionListener(l);
        dialog.show();

        return (retValue[0]);
    }
}

Related

  1. setSelectedSilently(AbstractButton x, boolean b)
  2. setToggleGroups(final JToggleButton... buttons)
  3. setToggling(AbstractButton b)
  4. setWithoutNotifyingListeners(AbstractButton button, boolean selected)
  5. showJFileChooser(javax.swing.JFileChooser chooser, java.awt.Component parent, java.lang.String approveButtonText)
  6. showOptions(int width, int type, String title, String text, Object... buttons)
  7. styleButton(final AbstractButton btn)
  8. toBold(final T button)
  9. tryToBuildAFocusGroup(AbstractButton... buttons)