Java JTextField showFileOpenDialogAndChangePrefs(String prefName, FileNameExtensionFilter fileNameExtensionFilter, JTextField textField, Class forClass, Component parent)

Here you can find the source of showFileOpenDialogAndChangePrefs(String prefName, FileNameExtensionFilter fileNameExtensionFilter, JTextField textField, Class forClass, Component parent)

Description

displays a file open dialog and copies the result to an edit field

License

Open Source License

Parameter

Parameter Description
prefName a parameter
fileNameExtensionFilter a parameter
textField a parameter
forClass a parameter
parent a parameter

Declaration

public static File showFileOpenDialogAndChangePrefs(String prefName,
        FileNameExtensionFilter fileNameExtensionFilter, JTextField textField, Class forClass,
        Component parent) 

Method Source Code


//package com.java2s;
/* /*from ww w.j  a  v a  2s  . c  o  m*/
 * Copyright (C) 2014 Institute for Bioinformatics and Systems Biology, University Giessen, Germany
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Component;

import java.io.File;

import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import javax.swing.JFileChooser;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Main {
    /**
     * displays a file open dialog and copies the result to an edit field
     * @param prefName
     * @param fileNameExtensionFilter
     * @param textField
     * @param forClass
     * @param parent
     * @return 
     */
    public static File showFileOpenDialogAndChangePrefs(String prefName,
            FileNameExtensionFilter fileNameExtensionFilter, JTextField textField, Class forClass,
            Component parent) {
        JFileChooser fc = new JFileChooser();
        fc.setFileFilter(fileNameExtensionFilter);
        Preferences prefs2 = Preferences.userNodeForPackage(forClass);
        String path = prefs2.get(prefName, null);
        if (path != null) {
            fc.setCurrentDirectory(new File(path));
        }
        int result = fc.showOpenDialog(parent);

        if (result == 0) {
            // file chosen
            File file = fc.getSelectedFile();

            if (file.canRead()) {
                Preferences prefs = Preferences.userNodeForPackage(forClass);
                prefs.put(prefName, file.getAbsolutePath());
                textField.setText(file.getAbsolutePath());
                try {
                    prefs.flush();
                } catch (BackingStoreException ex) {
                    Logger.getLogger(forClass.getName()).log(Level.SEVERE, null, ex);
                }
                return file;
            } else {
                Logger.getLogger(forClass.getName()).log(Level.WARNING, "Could not read file");
            }
        }
        return null;
    }
}

Related

  1. setModelValue(JTextField tf, String value)
  2. setText(JTextField jField, Object pObject, Object pDefault)
  3. setupNoCommitCheckbox(final JCheckBox addLogInformationCheckBox, final JTextField commitMessage, final JCheckBox noCommitCheckBox)
  4. setupTextField(JPanel panel, JLabel label, JTextField field, long val, String toolTip)
  5. setValue(JTextField f, String value)
  6. textAreaChangeListener(JTextField a, final ChangeListener list)
  7. toUpperCase(JTextField field)
  8. triggerTextField(JTextField textField, JCheckBox checkBox)
  9. upperText(JTextField text)