Java Reference
In-Depth Information
■
Caution
Don't confuse the abstract
javax.swing.filechooser.FileFilter
class with the
java.io.FileFilter
interface. Although functionally similar, they're different. The two coexist because
the
java.io.FileFilter
interface didn't exist in a Java 1.1 runtime. Because the original Swing
JFileChooser
needed to run in both Java 1.1 and Java 2 environments, the chooser needed to define a
replacement. Unless otherwise specified, all
FileFilter
references in this text are to the class in the
javax.swing.filechooser
package.
Creating a JFileChooser
There are six constructors for
JFileChooser
:
public JFileChooser()
JFileChooser fileChooser = new JFileChooser();
public JFileChooser(File currentDirectory)
File currentDirectory = new File("."); // starting directory of program
JFileChooser fileChooser = new JFileChooser(currentDirectory);
public JFileChooser(File currentDirectory, FileSystemView fileSystemView)
FileSystemView fileSystemView = new SomeFileSystemView(...);
JFileChooser fileChooser = new JFileChooser(currentDirectory, fileSystemView);
public JFileChooser(FileSystemView fileSystemView)
JFileChooser fileChooser = new JFileChooser(fileSystemView);
public JFileChooser(String currentDirectoryPath)
String currentDirectoryPath = "."; // starting directory of program
JFileChooser fileChooser = new JFileChooser(currentDirectoryPath);
public JFileChooser(String currentDirectoryPath, FileSystemView fileSystemView)
JFileChooser fileChooser = new JFileChooser(currentDirectoryPath, fileSystemView);
By default, the starting directory displayed is the user's home directory (system property
user.home
). If you want to start the
JFileChooser
pointing at another directory, the directory
can be specified as either a
String
or a
File
object.
You can also specify a
FileSystemView
to specify a custom representation to the operating
system's top-level directory structure. When the
FileSystemView
argument is not specified, the
JFileChooser
uses a
FileSystemView
appropriate for the user's operating system.
Using JFileChooser
After creating a
JFileChooser
from a constructor, you can place it in any
Container
, because it's
a
JComponent
. The
JFileChooser
object looks a little strange in an object that's not a pop-up
window, but this may allow you to do a task without needing to constantly bring up a new file
chooser.