Java JDialog addFileOnAction(AbstractButton button, final JTextField textField, final JDialog parent)

Here you can find the source of addFileOnAction(AbstractButton button, final JTextField textField, final JDialog parent)

Description

Add an action listener which presents a common file dialog.

License

Open Source License

Declaration

public static void addFileOnAction(AbstractButton button,
        final JTextField textField, final JDialog parent) 

Method Source Code

//package com.java2s;
/*//from w ww . j  a  v a 2  s  . co  m
 Copyright 2009 Peter Hofmann

 This file is part of Multifrac.

 Multifrac is free software: you can redistribute it and/or modify it
 under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 Multifrac is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Multifrac. If not, see <http://www.gnu.org/licenses/>.
 */

import javax.swing.*;
import javax.swing.filechooser.*;

import java.awt.event.*;
import java.io.*;

public class Main {
    /**
     * Add an action listener which presents a common file dialog.
     */
    public static void addFileOnAction(AbstractButton button,
            final JTextField textField, final JDialog parent) {
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();

                // try to set the old directory and file
                File old = new File(textField.getText());
                chooser.setSelectedFile(old);

                // set up filters
                FileNameExtensionFilter tiff = new FileNameExtensionFilter(
                        "TIFF (large images)", "tif", "tiff");
                FileNameExtensionFilter png = new FileNameExtensionFilter(
                        "PNG & JPG (regular images)", "png", "jpg");

                chooser.addChoosableFileFilter(png);
                chooser.addChoosableFileFilter(tiff);

                // choose current filter
                if (tiff.accept(old))
                    chooser.setFileFilter(tiff);
                else if (png.accept(old))
                    chooser.setFileFilter(png);
                else
                    chooser.setAcceptAllFileFilterUsed(true);

                // fire up the dialog
                int returnVal = chooser.showSaveDialog(parent);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    textField.setText(chooser.getSelectedFile()
                            .getAbsolutePath());
                }
            }
        });
    }
}

Related

  1. adaptSize(JDialog frame, int preferredWidth, int preferredHeight)
  2. alignDialogInMiddleOfScreen(JDialog dlg)
  3. applyDefaultProperties(final JDialog comp)
  4. calculatePosition(Window parent, JDialog dialog)
  5. cascadeDialog(JDialog dialog, JDialog parent)