JFileChooser provides a simple mechanism for the user to choose a file. For information about using JFileChooser, see How to Use File Choosers, a section in The Java Tutorial. The following code pops up a file chooser for the user's home directory that sees only .jpg and .gif images:
JFileChooser chooser = new JFileChooser();
// Note: source for ExampleFileFilter can be found in FileChooserDemo,
// under the demo/jfc directory in the Java 2 SDK, Standard Edition.
ExampleFileFilter filter = new ExampleFileFilter();
filter.addExtension("jpg");
filter.addExtension("gif");
filter.setDescription("JPG & GIF Images");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
}
| Field Detail |
private static final String uiClassID
public static final int OPEN_DIALOG
JFileChooser supports an "Open" file operation.public static final int SAVE_DIALOG
JFileChooser supports a "Save" file operation.public static final int CUSTOM_DIALOG
JFileChooser supports a developer-specified file operation.public static final int CANCEL_OPTION
public static final int APPROVE_OPTION
public static final int ERROR_OPTION
public static final int FILES_ONLY
public static final int DIRECTORIES_ONLY
public static final int FILES_AND_DIRECTORIES
public static final String CANCEL_SELECTION
public static final String APPROVE_SELECTION
public static final String APPROVE_BUTTON_TEXT_CHANGED_PROPERTY
public static final String APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY
public static final String APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY
public static final String CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY
public static final String DIRECTORY_CHANGED_PROPERTY
public static final String SELECTED_FILE_CHANGED_PROPERTY
public static final String SELECTED_FILES_CHANGED_PROPERTY
public static final String MULTI_SELECTION_ENABLED_CHANGED_PROPERTY
public static final String FILE_SYSTEM_VIEW_CHANGED_PROPERTY
public static final String FILE_VIEW_CHANGED_PROPERTY
public static final String FILE_HIDING_CHANGED_PROPERTY
public static final String FILE_FILTER_CHANGED_PROPERTY
public static final String FILE_SELECTION_MODE_CHANGED_PROPERTY
public static final String ACCESSORY_CHANGED_PROPERTY
public static final String ACCEPT_ALL_FILE_FILTER_USED_CHANGED_PROPERTY
public static final String DIALOG_TITLE_CHANGED_PROPERTY
public static final String DIALOG_TYPE_CHANGED_PROPERTY
public static final String CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY
| Constructor Detail |
public JFileChooser()
JFileChooser pointing to the user's default directory. This default depends on the operating system. It is typically the "My Documents" folder on Windows, and the user's home directory on Unix.public JFileChooser(String currentDirectoryPath)
JFileChooser using the given path. Passing in a null string causes the file chooser to point to the user's default directory. This default depends on the operating system. It is typically the "My Documents" folder on Windows, and the user's home directory on Unix.currentDirectoryPath - a String giving the path to a file or directorypublic JFileChooser(File currentDirectory)
JFileChooser using the given File as the path. Passing in a null file causes the file chooser to point to the user's default directory. This default depends on the operating system. It is typically the "My Documents" folder on Windows, and the user's home directory on Unix.currentDirectory - a File object specifying the path to a file or directorypublic JFileChooser(FileSystemView fsv)
JFileChooser using the given FileSystemView.
public JFileChooser(File currentDirectory,
FileSystemView fsv)
JFileChooser using the given current directory and FileSystemView.
public JFileChooser(String currentDirectoryPath,
FileSystemView fsv)
JFileChooser using the given current directory path and FileSystemView.| Method Detail |
protected void setup(FileSystemView view)
public void setDragEnabled(boolean b)
dragEnabled property, which must be true to enable automatic drag handling (the first part of drag and drop) on this component. The transferHandler property needs to be set to a non-null value for the drag to do anything. The default value of the dragEnabled property is false. When automatic drag handling is enabled, most look and feels begin a drag-and-drop operation whenever the user presses the mouse button over a selection and then moves the mouse a few pixels. Setting this property to true can therefore have a subtle effect on how selections behave.
Some look and feels might not support automatic drag and drop; they will ignore this property. You can work around such look and feels by modifying the component to directly call the exportAsDrag method of a TransferHandler.
b - the value to set the dragEnabled property toHeadlessException - if b is true and GraphicsEnvironment.isHeadless() returns truepublic boolean getDragEnabled()
dragEnabled property.dragEnabled propertypublic File getSelectedFile()
setFile or by a user action, such as either typing the filename into the UI or selecting the file from a list in the UI.public void setSelectedFile(File file)
file - the selected filepublic File[] getSelectedFiles()
public void setSelectedFiles(File[] selectedFiles)
public File getCurrentDirectory()
public void setCurrentDirectory(File dir)
null sets the file chooser to point to the user's default directory. This default depends on the operating system. It is typically the "My Documents" folder on Windows, and the user's home directory on Unix. If the file passed in as currentDirectory is not a directory, the parent of the file will be used as the currentDirectory. If the parent is not traversable, then it will walk up the parent tree until it finds a traversable directory, or hits the root of the file system.dir - the current directory to point topublic void changeToParentDirectory()
public void rescanCurrentDirectory()
public void ensureFileIsVisible(File f)
f - a File objectpublic int showOpenDialog(Component parent)
parent - the parent component of the dialog, can be null; see showDialog for detailsHeadlessException - if GraphicsEnvironment.isHeadless() returns true.public int showSaveDialog(Component parent)
parent - the parent component of the dialog, can be null; see showDialog for detailsHeadlessException - if GraphicsEnvironment.isHeadless() returns true.
public int showDialog(Component parent,
String approveButtonText)
filechooser.showDialog(parentFrame, "Run Application");Alternatively, the following code does the same thing:
JFileChooser chooser = new JFileChooser(null);
chooser.setApproveButtonText("Run Application");
chooser.showDialog(parentFrame, null);
The parent argument determines two things: the frame on which the open dialog depends and the component whose position the look and feel should consider when placing the dialog. If the parent is a Frame object (such as a JFrame) then the dialog depends on the frame and the look and feel positions the dialog relative to the frame (for example, centered over the frame). If the parent is a component, then the dialog depends on the frame containing the component, and is positioned relative to the component (for example, centered over the component). If the parent is null, then the dialog depends on no visible window, and it's placed in a look-and-feel-dependent position such as the center of the screen.
parent - the parent component of the dialog; can be nullapproveButtonText - the text of the ApproveButtonHeadlessException - if GraphicsEnvironment.isHeadless() returns true.protected JDialog createDialog(Component parent)
JDialog wrapping this centered on the parent in the parent's frame. This method can be overriden to further manipulate the dialog, to disable resizing, set the location, etc. Example:
class MyFileChooser extends JFileChooser {
protected JDialog createDialog(Component parent) throws HeadlessException {
JDialog dialog = super.createDialog(parent);
dialog.setLocation(300, 200);
dialog.setResizable(false);
return dialog;
}
}
parent - the parent component of the dialog; can be nullJDialog containing this instanceHeadlessException - if GraphicsEnvironment.isHeadless() returns true.public boolean getControlButtonsAreShown()
controlButtonsAreShown property.controlButtonsAreShown propertypublic void setControlButtonsAreShown(boolean b)
true by default. Look and feels that always show these buttons will ignore the value of this property. This method fires a property-changed event, using the string value of CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY as the name of the property.b - false if control buttons should not be shown; otherwise, truepublic int getDialogType()
JFileChooser.OPEN_DIALOG.public void setDialogType(int dialogType)
OPEN_DIALOG when you want to bring up a file chooser that the user can use to open a file. Likewise, use SAVE_DIALOG for letting the user choose a file for saving. Use CUSTOM_DIALOG when you want to use the file chooser in a context other than "Open" or "Save". For instance, you might want to bring up a file chooser that allows the user to choose a file to execute. Note that you normally would not need to set the JFileChooser to use CUSTOM_DIALOG since a call to setApproveButtonText does this for you. The default dialog type is JFileChooser.OPEN_DIALOG.dialogType - the type of dialog to be displayed: IllegalArgumentException - if dialogType is not legalpublic void setDialogTitle(String dialogTitle)
JFileChooser window's title bar.dialogTitle - the new String for the title barpublic String getDialogTitle()
JFileChooser's titlebar.public void setApproveButtonToolTipText(String toolTipText)
ApproveButton. If null, the UI object will determine the button's text.public String getApproveButtonToolTipText()
ApproveButton. If null, the UI object will determine the button's text.ApproveButtonpublic int getApproveButtonMnemonic()
public void setApproveButtonMnemonic(int mnemonic)
mnemonic - an integer value for the mnemonic keypublic void setApproveButtonMnemonic(char mnemonic)
mnemonic - a character value for the mnemonic keypublic void setApproveButtonText(String approveButtonText)
ApproveButton in the FileChooserUI.approveButtonText - the text used in the ApproveButtonpublic String getApproveButtonText()
ApproveButton in the FileChooserUI. If null, the UI object will determine the button's text. Typically, this would be "Open" or "Save".ApproveButtonpublic FileFilter[] getChoosableFileFilters()
FileFilter array containing all the choosable file filterspublic void addChoosableFileFilter(FileFilter filter)
filter - the FileFilter to add to the choosable file filter listpublic boolean removeChoosableFileFilter(FileFilter f)
public void resetChoosableFileFilters()
AcceptAll file filter.public FileFilter getAcceptAllFileFilter()
AcceptAll file filter. For example, on Microsoft Windows this would be All Files (*.*).public boolean isAcceptAllFileFilterUsed()
AcceptAll FileFilter is used.AcceptAll FileFilter is usedpublic void setAcceptAllFileFilterUsed(boolean b)
AcceptAll FileFilter is used as an available choice in the choosable filter list. If false, the AcceptAll file filter is removed from the list of available file filters. If true, the AcceptAll file filter will become the the actively used file filter.public JComponent getAccessory()
public void setAccessory(JComponent newAccessory)
Note: if there was a previous accessory, you should unregister any listeners that the accessory might have registered with the file chooser.
public void setFileSelectionMode(int mode)
JFileChooser to allow the user to just select files, just select directories, or select both files and directories. The default is JFilesChooser.FILES_ONLY.mode - the type of files to be displayed: IllegalArgumentException - if mode is an illegal Dialog modepublic int getFileSelectionMode()
JFilesChooser.FILES_ONLY.public boolean isFileSelectionEnabled()
public boolean isDirectorySelectionEnabled()
public void setMultiSelectionEnabled(boolean b)
b - true if multiple files may be selectedpublic boolean isMultiSelectionEnabled()
public boolean isFileHidingEnabled()
public void setFileHidingEnabled(boolean b)
FileView.b - the boolean value that determines whether file hiding is turned onpublic void setFileFilter(FileFilter filter)
filter - the new current file filter to usepublic FileFilter getFileFilter()
public void setFileView(FileView fileView)
public FileView getFileView()
public String getName(File f)
f - the FileString containing the filename for fpublic String getDescription(File f)
f - the FileString containing the file description for fpublic String getTypeDescription(File f)
f - the FileString containing the file type description for fpublic Icon getIcon(File f)
f - the FileIcon for this file, or type of filepublic boolean isTraversable(File f)
f - the Filepublic boolean accept(File f)
f - the Filepublic void setFileSystemView(FileSystemView fsv)
JFileChooser uses for accessing and creating file system resources, such as finding the floppy drive and getting a list of root drives.fsv - the new FileSystemViewpublic FileSystemView getFileSystemView()
FileSystemView objectpublic void approveSelection()
APPROVE_SELECTION.public void cancelSelection()
CANCEL_SELECTION.public void addActionListener(ActionListener l)
ActionListener to the file chooser.l - the listener to be addedpublic void removeActionListener(ActionListener l)
ActionListener from the file chooser.l - the listener to be removedpublic ActionListener[] getActionListeners()
ActionListeners or an empty array if no action listeners are currently registeredprotected void fireActionPerformed(String command)
command parameter.public void updateUI()
public String getUIClassID()
public FileChooserUI getUI()
private void writeObject(ObjectOutputStream s)
readObject and writeObject in JComponent for more information about serialization in Swing.protected String paramString()
JFileChooser. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be null.JFileChooserpublic AccessibleContext getAccessibleContext()