2
* This program is free software; you can redistribute it and/or modify
3
* it under the terms of the GNU General Public License as published by
4
* the Free Software Foundation; either version 2 of the License, or
5
* (at your option) any later version.
6
* This program is distributed in the hope that it will be useful,
7
* but WITHOUT ANY WARRANTY; without even the implied warranty of
8
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9
* GNU General Public License for more details.
10
* You should have received a copy of the GNU General Public License
11
* along with this program; if not, write to the Free Software
12
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
@author George El-Haddad
18
@email nekromancer@users.sourceforge.net
28
private final String ERR_MSG_1 = "Warning! Could not load the file!";
30
private File file = null;
35
@param java.lang.String title the title of the JFileChooser
37
@exception java.lang.Exception ex
39
Creates a file selection dialog starting at the current
40
working directory. Filters out all non-txt files and
41
handles possible file load errors.
43
public FileOpen(String title)
45
this(title, ".txt", ".gif", ".png");
49
public FileOpen(String title, final String... extensions)
51
JFileChooser chooser = new JFileChooser();
52
chooser.setDialogTitle(title);
53
chooser.setCurrentDirectory(new File(System.getProperties().getProperty("user.dir")));
56
* Show only text and gif files
58
chooser.setFileFilter(new javax.swing.filechooser.FileFilter()
60
public boolean accept(File f)
66
for(String ex : extensions)
68
if(f.getName().endsWith(ex)) {
75
public String getDescription()
77
StringBuffer buf = new StringBuffer();
78
for(String ex : extensions)
80
buf.append("*").append(ex).append(" ");
82
return buf.toString();
86
int result = chooser.showOpenDialog(null);
88
if(result == JFileChooser.CANCEL_OPTION)
95
file = chooser.getSelectedFile(); //get the file
99
JOptionPane.showMessageDialog(null,ERR_MSG_1,"Warning!", JOptionPane.WARNING_MESSAGE);
107
Returns the newly selected file.
108
Will return null if no file is selected.
112
public File getFile()
119
Returns the newly selected file.
120
Will return null if no file is selected.
122
@return java.lang.String
124
public String getPathString()
132
return file.getPath();
136
}//end class FileOpen