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();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif");
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());
}
Warning
Swing is not thread safe. For more information see Swing's Threading Policy.
| Modifier and Type | Class and Description |
|---|---|
| protected class | JFileChooser.
This class implements accessibility support for the
|
| private class | |
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| public static final String | ACCEPT_ALL_FILE_FILTER_USED_CHANGED_PROPERTY
Identifies whether the AcceptAllFileFilter is used or not. |
| protected AccessibleContext | accessibleContext
Hides java. AccessibleContext associated with this JFileChooser
|
| private JComponent | |
| public static final String | ACCESSORY_CHANGED_PROPERTY
Says that a different accessory component is in use (for example, to preview files). |
| public static final String | APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY
Identifies change in the mnemonic for the approve (yes, ok) button. |
| public static final String | APPROVE_BUTTON_TEXT_CHANGED_PROPERTY
Identifies change in the text on the approve (yes, ok) button. |
| public static final String | APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY
Identifies change in the tooltip text for the approve (yes, ok) button. |
| public static final int | APPROVE_OPTION
Return value if approve (yes, ok) is chosen. |
| public static final String | APPROVE_SELECTION
Instruction to approve the current selection (same as pressing yes or ok). |
| private int | |
| private String | |
| private String | |
| public static final int | CANCEL_OPTION
Return value if cancel is chosen. |
| public static final String | CANCEL_SELECTION
Instruction to cancel the current selection. |
| public static final String | CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY
Identifies a change in the list of predefined file filters the user can choose from. |
| public static final String | CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY
Instruction to display the control buttons. |
| private boolean | |
| private File | |
| public static final int | CUSTOM_DIALOG
Type value indicating that the |
| private JDialog | |
| public static final String | DIALOG_TITLE_CHANGED_PROPERTY
Identifies a change in the dialog title. |
| public static final String | DIALOG_TYPE_CHANGED_PROPERTY
Identifies a change in the type of files displayed (files only, directories only, or both files and directories). |
| private String | |
| private int | |
| public static final int | DIRECTORIES_ONLY
Instruction to display only directories. |
| public static final String | DIRECTORY_CHANGED_PROPERTY
Identifies user's directory change. |
| private boolean | |
| public static final int | ERROR_OPTION
Return value if an error occurred. |
| public static final String | FILE_FILTER_CHANGED_PROPERTY
User changed the kind of files to display. |
| public static final String | FILE_HIDING_CHANGED_PROPERTY
Identifies a change in the display-hidden-files property. |
| public static final String | FILE_SELECTION_MODE_CHANGED_PROPERTY
Identifies a change in the kind of selection (single, multiple, etc.). |
| public static final String | FILE_SYSTEM_VIEW_CHANGED_PROPERTY
Says that a different object is being used to find available drives on the system. |
| public static final String | FILE_VIEW_CHANGED_PROPERTY
Says that a different object is being used to retrieve file information. |
| private FileFilter | |
| public static final int | FILES_AND_DIRECTORIES
Instruction to display both files and directories. |
| public static final int | FILES_ONLY
Instruction to display only files. |
| private int | |
| private FileSystemView | |
| private FileView | |
| private Vector | |
| public static final String | MULTI_SELECTION_ENABLED_CHANGED_PROPERTY
Enables multiple-file selections. |
| private boolean | |
| public static final int | OPEN_DIALOG
Type value indicating that the |
| private int | |
| public static final int | SAVE_DIALOG
Type value indicating that the |
| public static final String | SELECTED_FILE_CHANGED_PROPERTY
Identifies change in user's single-file selection. |
| public static final String | SELECTED_FILES_CHANGED_PROPERTY
Identifies change in user's multiple-file selection. |
| private File | |
| private File[] | |
| private static final String | |
| private transient PropertyChangeListener | |
| private static final String | |
| private boolean | |
| private boolean |
| Access | Constructor and Description |
|---|---|
| public | |
| public | JFileChooser(String
a currentDirectoryPath)String giving the path
to a file or directoryConstructs a |
| public | JFileChooser(File
a currentDirectory)File object specifying
the path to a file or directoryConstructs a |
| public | JFileChooser(FileSystemView
a fsv)FileSystemViewConstructs a |
| public | JFileChooser(File
a currentDirectory, FileSystemView File object specifying the path to a
file or directorya fsv)FileSystemViewConstructs a |
| public | JFileChooser(String
a currentDirectoryPath, FileSystemView String specifying the path to a file
or directorya fsv)FileSystemViewConstructs a |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | |
| public void | addActionListener(ActionListener
the listener to be added l)Adds an |
| public void | addChoosableFileFilter(FileFilter
the filter)FileFilter to add to the choosable file
filter listAdds a filter to the list of user choosable file filters. |
| public void | approveSelection()
Called by the UI when the user hits the Approve button (labeled "Open" or "Save", by default). |
| public void | |
| public void | |
| private static void | |
| private static void | |
| private static void | |
| protected JDialog | Returns: a newJDialog containing this instancethe parent component of the dialog;
can be parent)nullCreates and returns a new |
| public void | ensureFileIsVisible(File
a File object f)Makes sure that the specified file is viewable, and not hidden. |
| protected void | fireActionPerformed(String
a string that may specify a command associated with
the event command)Notifies all listeners that have registered interest for notification on this event type. |
| public FileFilter | |
| public AccessibleContext | Returns: an AccessibleJFileChooser that serves as the AccessibleContext of this JFileChooserOverrides java. Implements javax. Gets the AccessibleContext associated with this JFileChooser. |
| public JComponent | Returns: this JFileChooser's accessory component, or nullReturns the accessory component. |
| public ActionListener[] | Returns: all of this file chooser'sActionListeners
or an empty
array if no action listeners are currently registeredReturns an array of all the action listeners registered on this file chooser. |
| public int | Returns: an integer value for the mnemonic keyReturns the approve button's mnemonic. |
| public String | Returns: the text used in theApproveButtonReturns the text used in the |
| public String | Returns: the tooltip text used for the approve buttonReturns the tooltip text used in the |
| public FileFilter[] | Returns: aFileFilter array containing all the choosable
file filtersGets the list of user choosable file filters. |
| public boolean | Returns: the value of thecontrolButtonsAreShown
propertyReturns the value of the |
| public File | |
| public String | Returns: theString containing the file description for
fthe f)FileReturns the file description. |
| public String | Returns: the string from theJFileChooser window's title barGets the string that goes in the |
| public int | Returns: the type of dialog to be displayed:
Returns the type of this dialog. |
| public boolean | Returns: the value of thedragEnabled propertyGets the value of the |
| public FileFilter | |
| public int | Returns: the type of files to be displayed, one of the following:
Returns the current file-selection mode. |
| public FileSystemView | |
| public FileView | |
| public Icon | |
| public String | |
| public File | |
| public File[] | Returns: an array of selectedFilesReturns a list of selected files if the file chooser is set to allow multiple selection. |
| public String | Returns: theString containing the file type description for
fthe f)FileReturns the file type. |
| public FileChooserUI | Returns: the FileChooserUI object that implements the FileChooserUI L&FOverrides javax. Gets the UI object which implements the L&F for this component. |
| public String | Returns: the string "FileChooserUI"Overrides javax. Returns a string that specifies the name of the L&F class that renders this component. |
| private void | |
| private void | |
| public boolean | Returns: true if theAcceptAll FileFilter is usedReturns whether the |
| public boolean | Returns: true if directories are selectable, false otherwiseConvenience call that determines if directories are selectable based on the current file selection mode. |
| public boolean | Returns: the status of the file hiding propertyReturns true if hidden files are not shown in the file chooser; otherwise, returns false. |
| public boolean | Returns: true if files are selectable, false otherwiseConvenience call that determines if files are selectable based on the current file selection mode. |
| public boolean | Returns: true if multiple files can be selectedReturns true if multiple files can be selected. |
| public boolean | Returns: true if the file/directory can be traversed, otherwise falsethe f)FileReturns true if the file (directory) can be visited. |
| protected String | Returns: a string representation of thisJFileChooserOverrides javax. Returns a string representation of this |
| private void | readObject(ObjectInputStream in)
Hides javax. See |
| public void | removeActionListener(ActionListener
the listener to be removed l)Removes an |
| public boolean | Returns: true if the file filter was removed, false otherwisethe file filter to be removed f)Removes a filter from the list of user choosable file filters. |
| public void | |
| public void | |
| public void | setAcceptAllFileFilterUsed(boolean
a b)boolean which determines whether the AcceptAll
file filter is an available choice in the choosable filter listDetermines whether the |
| public void | |
| public void | setApproveButtonMnemonic(int
an integer value for the mnemonic key mnemonic)Sets the approve button's mnemonic using a numeric keycode. |
| public void | setApproveButtonMnemonic(char
a character value for the mnemonic key mnemonic)Sets the approve button's mnemonic using a character. |
| public void | setApproveButtonText(String
the text used in the approveButtonText)ApproveButtonSets the text used in the |
| public void | setApproveButtonToolTipText(String
the tooltip text for the approve button toolTipText)Sets the tooltip text used in the |
| public void | setControlButtonsAreShown(boolean
false if control buttons should not be
shown; otherwise, trueSets the property that indicates whether the approve and cancel buttons are shown in the file chooser. |
| public void | |
| public void | setDialogTitle(String
the new dialogTitle)String for the title barSets the string that goes in the |
| public void | setDialogType(int
the type of dialog to be displayed:
dialogType)
Sets the type of this dialog. |
| public void | setDragEnabled(boolean
the value to set the b)dragEnabled property toSets the |
| public void | setEnabled(boolean enabled)
Overrides javax. Sets whether or not this component is enabled. |
| private static void | |
| public void | |
| public void | setFileHidingEnabled(boolean
the boolean value that determines whether file hiding is
turned on b)Sets file hiding on or off. |
| public void | setFileSelectionMode(int
the type of files to be displayed:
mode)
Sets the |
| public void | setFileSystemView(FileSystemView
the new fsv)FileSystemViewSets the file system view that the |
| public void | setFileView(FileView
a fileView)FileView to be used to retrieve UI informationSets the file view to be used to retrieve UI information, such as the icon that represents a file or the type description of a file. |
| public void | setMultiSelectionEnabled(boolean
true if multiple files may be selected b)Sets the file chooser to allow multiple file selections. |
| public void | |
| public void | setSelectedFiles(File[]
an array selectedFiles)Files to be selectedSets the list of selected files if the file chooser is set to allow multiple selection. |
| protected void | setup(FileSystemView
the view)FileSystemView used for setupPerforms common constructor initialization and setup. |
| public int | Returns: the return state of the file chooser on popdown:
the parent component of the dialog;
can be parent, String nullthe text of the approveButtonText)ApproveButtonPops a custom file chooser dialog with a custom approve button. |
| public int | Returns: the return state of the file chooser on popdown:
the parent component of the dialog,
can be parent)null;
see showDialog for detailsPops up an "Open File" file chooser dialog. |
| public int | Returns: the return state of the file chooser on popdown:
the parent component of the dialog,
can be parent)null;
see showDialog for detailsPops up a "Save File" file chooser dialog. |
| public void | updateUI()
Overrides javax. Resets the UI property to a value from the current look and feel. |
| private void | writeObject(ObjectOutputStream s)
Hides javax. See |