Here you can find the source of showFileOpenDialogAndChangePrefs(String prefName, FileNameExtensionFilter fileNameExtensionFilter, JTextField textField, Class forClass, Component parent)
Parameter | Description |
---|---|
prefName | a parameter |
fileNameExtensionFilter | a parameter |
textField | a parameter |
forClass | a parameter |
parent | a parameter |
public static File showFileOpenDialogAndChangePrefs(String prefName, FileNameExtensionFilter fileNameExtensionFilter, JTextField textField, Class forClass, Component parent)
//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; } }