Package com.anlavn.ui.filechooser
Class WindowsFileChooser
java.lang.Object
com.anlavn.ui.filechooser.WindowsFileChooser
The native Windows file chooser dialog.
Example:
WindowsFileChooser fc = new WindowsFileChooser("C:\\");
fc.addFilter("All Files", "*");
fc.addFilter("Text files", "txt", "log", "xml", "css", "html");
fc.addFilter("Source code", "java", "c", "cpp", "cc", "h", "hpp");
fc.addFilter("Binary files", "exe", "class", "jar", "dll", "so");
if (fc.showOpenDialog(parent)) {
File f = fc.getSelectedFile();
// do something with f
}
Note that although you can set the initial directory Windows will
determine the initial directory according to the following rules
(the initial directory is referred to as "lpstrInitialDir"):
Windows 7:
1. If lpstrInitialDir has the same value as was passed the first time the
application used an Open or Save As dialog box, the path most recently
selected by the user is used as the initial directory.
2. Otherwise, if lpstrFile contains a path, that path is the initial
directory.
3. Otherwise, if lpstrInitialDir is not NULL, it specifies the initial
directory.
4. If lpstrInitialDir is NULL and the current directory contains any files of
the specified filter types, the initial directory is the current
directory.
5. Otherwise, the initial directory is the personal files directory of the
current user.
6. Otherwise, the initial directory is the Desktop folder.
Windows 2000/XP/Vista:
1. If lpstrFile contains a path, that path is the initial directory.
2. Otherwise, lpstrInitialDir specifies the initial directory.
3. Otherwise, if the application has used an Open or Save As dialog box in
the past, the path most recently used is selected as the initial
directory. However, if an application is not run for a long time, its
saved selected path is discarded.
4. If lpstrInitialDir is NULL and the current directory contains any files
of the specified filter types, the initial directory is the current
directory.
5. Otherwise, the initial directory is the personal files directory of the
current user.
6. Otherwise, the initial directory is the Desktop folder.
Therefore you probably want to use an exe wrapper like WinRun4J in order
for this to work properly on Windows 7. Otherwise multiple programs may
interfere with each other. Unfortunately there doesn't seem to be a way
to override this behaviour.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptioncreates a new file chooserWindowsFileChooser(File currentDirectory) creates a new file chooser with the specified initial directory If the given file is not a directory the parent file will be used instead.WindowsFileChooser(String currentDirectoryPath) creates a new file chooser with the specified initial directory path -
Method Summary
Modifier and TypeMethodDescriptionvoidadd a filter to the user-selectable list of file filtersreturns the current directory This is always the parent directory of the chosen file, even if you enter an absolute path to a file that doesn't exist in the current directory.returns the file selected by the uservoidsetDefaultFilename(String defaultFilename) voidset a title namebooleanshowOpenDialog(Window parent) show the dialog for opening a filebooleanshowSaveDialog(Window parent) show the dialog for saving a file
-
Field Details
-
selectedFile
-
currentDirectory
-
filters
-
defaultFilename
-
dialogTitle
-
-
Constructor Details
-
WindowsFileChooser
public WindowsFileChooser()creates a new file chooser -
WindowsFileChooser
creates a new file chooser with the specified initial directory If the given file is not a directory the parent file will be used instead.- Parameters:
currentDirectory- the initial directory
-
WindowsFileChooser
creates a new file chooser with the specified initial directory path- Parameters:
currentDirectoryPath- the initial directory path; may be null
-
-
Method Details
-
addFilter
add a filter to the user-selectable list of file filters- Parameters:
name- name of the filterfilter- you must pass at least 1 argument, the arguments are the file extensions.
-
setTitle
set a title name- Parameters:
tname- of dialog
-
showOpenDialog
show the dialog for opening a file- Parameters:
parent- the parent window of the dialog- Returns:
- true if the user clicked ok, false otherwise
-
showSaveDialog
show the dialog for saving a file- Parameters:
parent- the parent window of the dialog- Returns:
- true if the user clicked ok, false otherwise
-
getSelectedFile
returns the file selected by the user- Returns:
- the selected file; null if the dialog was canceled or never shown
-
getCurrentDirectory
returns the current directory This is always the parent directory of the chosen file, even if you enter an absolute path to a file that doesn't exist in the current directory.- Returns:
- the current directory
-
setDefaultFilename
-