APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.swing

public Class JFileChooser

extends JComponent
implements Accessible
Class Inheritance
All Implemented Interfaces
javax.accessibility.Accessible
Known Direct Subclasses
sun.print.ServiceDialog.ValidatingFileChooser
Annotations
@JavaBean
defaultProperty:UI
description:A component which allows for the interactive selection of a file.
@SwingContainer:false
@SuppressWarnings:serial
Imports
java.awt.AWTEvent, .BorderLayout, .Component, .Container, .Dialog, .EventQueue, .Frame, .GraphicsEnvironment, .HeadlessException, .Toolkit, .Window, java.awt.event.ActionEvent, .ActionListener, .HierarchyEvent, .HierarchyListener, .InputEvent, .WindowAdapter, .WindowEvent, java.beans.BeanProperty, .JavaBean, .PropertyChangeEvent, .PropertyChangeListener, java.io.File, .IOException, .InvalidObjectException, .ObjectInputStream, .ObjectOutputStream, .Serial, .Serializable, java.lang.ref.WeakReference, java.util.ArrayList, .Vector, javax.accessibility.Accessible, .AccessibleContext, .AccessibleRole, javax.swing.event.EventListenerList, javax.swing.filechooser.FileFilter, .FileSystemView, .FileView, javax.swing.plaf.FileChooserUI

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.

Author
Jeff Dinkins
Since
1.2

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
JFileChooser.AccessibleJFileChooser

This class implements accessibility support for the JFileChooser class.

private class
private static class

Field Summary

Modifier and TypeField 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.awt.Component.accessibleContext.

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 JFileChooser supports a developer-specified file operation.

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<FileFilter>
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 JFileChooser supports an "Open" file operation.

private int
public static final int
SAVE_DIALOG

Type value indicating that the JFileChooser supports a "Save" file operation.

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
Inherited from javax.swing.JComponent:
DEBUG_GRAPHICS_LOADEDfocusControllerivSourceComponentlistenerListpaintingChildTOOL_TIP_TEXT_KEYuiUNDEFINED_CONDITIONWHEN_ANCESTOR_OF_FOCUSED_COMPONENTWHEN_FOCUSEDWHEN_IN_FOCUSED_WINDOW

Constructor Summary

AccessConstructor and Description
public
JFileChooser()

Constructs a JFileChooser pointing to the user's default directory.

public
JFileChooser(String
a String giving the path to a file or directory
currentDirectoryPath
)

Constructs a JFileChooser using the given path.

public
JFileChooser(File
a File object specifying the path to a file or directory
currentDirectory
)

Constructs a JFileChooser using the given File as the path.

public
JFileChooser(FileSystemView
a FileSystemView
fsv
)

Constructs a JFileChooser using the given FileSystemView.

public
JFileChooser(File
a File object specifying the path to a file or directory
currentDirectory
,
FileSystemView
a FileSystemView
fsv
)

Constructs a JFileChooser using the given current directory and FileSystemView.

public
JFileChooser(String
a String specifying the path to a file or directory
currentDirectoryPath
,
FileSystemView
a FileSystemView
fsv
)

Constructs a JFileChooser using the given current directory path and FileSystemView.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if the file should be displayed, otherwise false
accept
(File
the File
f
)

Returns true if the file should be displayed.

public void
addActionListener(ActionListener
the listener to be added
l
)

Adds an ActionListener to the file chooser.

public void
addChoosableFileFilter(FileFilter
the FileFilter to add to the choosable file filter list
filter
)

Adds 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
cancelSelection()

Called by the UI when the user chooses the Cancel button.

public void
changeToParentDirectory()

Changes the directory to be set to the parent of the current directory.

private static void
checkDialogType(int dialogType)

private static void
checkDragEnabled(boolean b)

private static void
protected JDialog

Returns:

a new JDialog containing this instance
createDialog
(Component
the parent component of the dialog; can be null
parent
)

Creates and returns a new JDialog wrapping this centered on the parent in the parent's frame.

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

Returns:

the AcceptAll file filter
getAcceptAllFileFilter
()

Returns the AcceptAll file filter.

public AccessibleContext

Returns:

an AccessibleJFileChooser that serves as the AccessibleContext of this JFileChooser
getAccessibleContext
()

Overrides java.awt.Component.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

Gets the AccessibleContext associated with this JFileChooser.

public JComponent

Returns:

this JFileChooser's accessory component, or null
getAccessory
()

Returns the accessory component.

public ActionListener[]

Returns:

all of this file chooser's ActionListeners or an empty array if no action listeners are currently registered
getActionListeners
()

Returns an array of all the action listeners registered on this file chooser.

public int

Returns:

an integer value for the mnemonic key
getApproveButtonMnemonic
()

Returns the approve button's mnemonic.

public String

Returns:

the text used in the ApproveButton
getApproveButtonText
()

Returns the text used in the ApproveButton in the FileChooserUI.

public String

Returns:

the tooltip text used for the approve button
getApproveButtonToolTipText
()

Returns the tooltip text used in the ApproveButton.

public FileFilter[]

Returns:

a FileFilter array containing all the choosable file filters
getChoosableFileFilters
()

Gets the list of user choosable file filters.

public boolean

Returns:

the value of the controlButtonsAreShown property
getControlButtonsAreShown
()

Returns the value of the controlButtonsAreShown property.

public File

Returns:

the current directory
getCurrentDirectory
()

Returns the current directory.

public String

Returns:

the String containing the file description for f
getDescription
(File
the File
f
)

Returns the file description.

public String

Returns:

the string from the JFileChooser window's title bar
getDialogTitle
()

Gets the string that goes in the JFileChooser's titlebar.

public int

Returns:

the type of dialog to be displayed:
  • JFileChooser.OPEN_DIALOG
  • JFileChooser.SAVE_DIALOG
  • JFileChooser.CUSTOM_DIALOG
getDialogType
()

Returns the type of this dialog.

public boolean

Returns:

the value of the dragEnabled property
getDragEnabled
()

Gets the value of the dragEnabled property.

public FileFilter

Returns:

the current file filter
getFileFilter
()

Returns the currently selected file filter.

public int

Returns:

the type of files to be displayed, one of the following:
  • JFileChooser.FILES_ONLY
  • JFileChooser.DIRECTORIES_ONLY
  • JFileChooser.FILES_AND_DIRECTORIES
getFileSelectionMode
()

Returns the current file-selection mode.

public FileSystemView

Returns:

the FileSystemView object
getFileSystemView
()

Returns the file system view.

public FileView

Returns:

the current file view
getFileView
()

Returns the current file view.

public Icon

Returns:

the Icon for this file, or type of file
getIcon
(File
the File
f
)

Returns the icon for this file or type of file, depending on the system.

public String

Returns:

the String containing the filename for f
getName
(File
the File
f
)

Returns the filename.

public File

Returns:

the selected file
getSelectedFile
()

Returns the selected file.

public File[]

Returns:

an array of selected Files
getSelectedFiles
()

Returns a list of selected files if the file chooser is set to allow multiple selection.

public String

Returns:

the String containing the file type description for f
getTypeDescription
(File
the File
f
)

Returns the file type.

public FileChooserUI

Returns:

the FileChooserUI object that implements the FileChooserUI L&F
getUI
()

Overrides javax.swing.JComponent.getUI.

Gets the UI object which implements the L&F for this component.

public String

Returns:

the string "FileChooserUI"
getUIClassID
()

Overrides javax.swing.JComponent.getUIClassID.

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 the AcceptAll FileFilter is used
isAcceptAllFileFilterUsed
()

Returns whether the AcceptAll FileFilter is used.

public boolean

Returns:

true if directories are selectable, false otherwise
isDirectorySelectionEnabled
()

Convenience call that determines if directories are selectable based on the current file selection mode.

public boolean

Returns:

the status of the file hiding property
isFileHidingEnabled
()

Returns true if hidden files are not shown in the file chooser; otherwise, returns false.

public boolean

Returns:

true if files are selectable, false otherwise
isFileSelectionEnabled
()

Convenience call that determines if files are selectable based on the current file selection mode.

public boolean

Returns:

true if multiple files can be selected
isMultiSelectionEnabled
()

Returns true if multiple files can be selected.

public boolean

Returns:

true if the file/directory can be traversed, otherwise false
isTraversable
(File
the File
f
)

Returns true if the file (directory) can be visited.

protected String

Returns:

a string representation of this JFileChooser
paramString
()

Overrides javax.swing.JComponent.paramString.

Returns a string representation of this JFileChooser.

private void
readObject(ObjectInputStream in)

Hides javax.swing.JComponent.readObject.

See readObject and writeObject in JComponent for more information about serialization in Swing.

public void
removeActionListener(ActionListener
the listener to be removed
l
)

Removes an ActionListener from the file chooser.

public boolean

Returns:

true if the file filter was removed, false otherwise
removeChoosableFileFilter
(FileFilter
the file filter to be removed
f
)

Removes a filter from the list of user choosable file filters.

public void
rescanCurrentDirectory()

Tells the UI to rescan its files list from the current directory.

public void
resetChoosableFileFilters()

Resets the choosable file filter list to its starting state.

public void
setAcceptAllFileFilterUsed(boolean
a boolean which determines whether the AcceptAll file filter is an available choice in the choosable filter list
b
)

Determines whether the AcceptAll FileFilter is used as an available choice in the choosable filter list.

public void
setAccessory(JComponent
the accessory component to be set
newAccessory
)

Sets the accessory component.

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 ApproveButton
approveButtonText
)

Sets the text used in the ApproveButton in the FileChooserUI.

public void
setApproveButtonToolTipText(String
the tooltip text for the approve button
toolTipText
)

Sets the tooltip text used in the ApproveButton.

public void
setControlButtonsAreShown(boolean
false if control buttons should not be shown; otherwise, true
b
)

Sets the property that indicates whether the approve and cancel buttons are shown in the file chooser.

public void
setCurrentDirectory(File
the current directory to point to
dir
)

Sets the current directory.

public void
setDialogTitle(String
the new String for the title bar
dialogTitle
)

Sets the string that goes in the JFileChooser window's title bar.

public void
setDialogType(int
the type of dialog to be displayed:
  • JFileChooser.OPEN_DIALOG
  • JFileChooser.SAVE_DIALOG
  • JFileChooser.CUSTOM_DIALOG
dialogType
)

Sets the type of this dialog.

public void
setDragEnabled(boolean
the value to set the dragEnabled property to
b
)

Sets the dragEnabled property, which must be true to enable automatic drag handling (the first part of drag and drop) on this component.

public void
setEnabled(boolean enabled)

Overrides javax.swing.JComponent.setEnabled.

Sets whether or not this component is enabled.

private static void
setEnabled(Container container, boolean enabled)

public void
setFileFilter(FileFilter
the new current file filter to use
filter
)

Sets the current file filter.

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:
  • JFileChooser.FILES_ONLY
  • JFileChooser.DIRECTORIES_ONLY
  • JFileChooser.FILES_AND_DIRECTORIES
mode
)

Sets the JFileChooser to allow the user to just select files, just select directories, or select both files and directories.

public void
setFileSystemView(FileSystemView
the new FileSystemView
fsv
)

Sets the file system view that the JFileChooser uses for accessing and creating file system resources, such as finding the floppy drive and getting a list of root drives.

public void
setFileView(FileView
a FileView to be used to retrieve UI information
fileView
)

Sets 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
setSelectedFile(File
the selected file
file
)

Sets the selected file.

public void
setSelectedFiles(File[]
an array Files to be selected
selectedFiles
)

Sets the list of selected files if the file chooser is set to allow multiple selection.

protected void
setup(FileSystemView
the FileSystemView used for setup
view
)

Performs common constructor initialization and setup.

public int

Returns:

the return state of the file chooser on popdown:
  • JFileChooser.CANCEL_OPTION
  • JFileChooser.APPROVE_OPTION
  • JFileChooser.ERROR_OPTION if an error occurs or the dialog is dismissed
showDialog
(Component
the parent component of the dialog; can be null
parent
,
String
the text of the ApproveButton
approveButtonText
)

Pops a custom file chooser dialog with a custom approve button.

public int

Returns:

the return state of the file chooser on popdown:
  • JFileChooser.CANCEL_OPTION
  • JFileChooser.APPROVE_OPTION
  • JFileChooser.ERROR_OPTION if an error occurs or the dialog is dismissed
showOpenDialog
(Component
the parent component of the dialog, can be null; see showDialog for details
parent
)

Pops up an "Open File" file chooser dialog.

public int

Returns:

the return state of the file chooser on popdown:
  • JFileChooser.CANCEL_OPTION
  • JFileChooser.APPROVE_OPTION
  • JFileChooser.ERROR_OPTION if an error occurs or the dialog is dismissed
showSaveDialog
(Component
the parent component of the dialog, can be null; see showDialog for details
parent
)

Pops up a "Save File" file chooser dialog.

public void
updateUI()

Overrides javax.swing.JComponent.updateUI.

Resets the UI property to a value from the current look and feel.

private void
writeObject(ObjectOutputStream s)

Hides javax.swing.JComponent.writeObject.

See readObject and writeObject in JComponent for more information about serialization in Swing.

Inherited from javax.swing.JComponent:
_paintImmediatelyaddAncestorListeneraddNotifyaddVetoableChangeListeneralwaysOnTopcheckIfChildObscuredBySiblingclientPropertyChangedcomponentInputMapChangedcomputeVisibleRectcomputeVisibleRectcompWriteObjectNotifycontainscreateToolTipdisabledndDonedropLocationForPointenablefirePropertyChangefirePropertyChangefirePropertyChangefireVetoableChangegetActionForKeyStrokegetActionMapgetActionMapgetAlignmentXgetAlignmentYgetAncestorListenersgetAutoscrollsgetBaselinegetBaselineResizeBehaviorgetBordergetBoundsgetClientPropertygetComponentGraphicsgetComponentPopupMenugetConditionForKeyStrokegetCreatedDoubleBuffergetDebugGraphicsOptionsgetDefaultLocalegetFontMetricsgetGraphicsgetGraphicsInvokedgetHeightgetInheritsPopupMenugetInputMapgetInputMapgetInputMapgetInputVerifiergetInsetsgetInsetsgetListenersgetLocationgetManagingFocusBackwardTraversalKeysgetManagingFocusForwardTraversalKeysgetMaximumSizegetMinimumSizegetNextFocusableComponentgetPopupLocationgetPreferredSizegetRegisteredKeyStrokesgetRootPanegetSizegetToolTipLocationgetToolTipTextgetToolTipTextgetTopLevelAncestorgetTransferHandlergetVerifyInputWhenFocusTargetgetVetoableChangeListenersgetVisibleRectgetWidthgetWriteObjCountergetXgetYgrabFocushideisDoubleBufferedisLightweightComponentisManagingFocusisOpaqueisOptimizedDrawingEnabledisPaintingisPaintingForPrintisPaintingOriginisPaintingTileisRequestFocusEnabledisValidateRootpaintpaintBorderpaintChildrenpaintComponentpaintForceDoubleBufferedpaintImmediatelypaintImmediatelypaintToOffscreenprintprintAllprintBorderprintChildrenprintComponentprocessComponentKeyEventprocessKeyBindingprocessKeyBindingsprocessKeyBindingsForAllComponentsprocessKeyEventprocessMouseEventprocessMouseMotionEventputClientPropertyrectangleIsObscuredregisterKeyboardActionregisterKeyboardActionremoveAncestorListenerremoveNotifyremoveVetoableChangeListenerrepaintrepaintrequestDefaultFocusrequestFocusrequestFocusrequestFocusInWindowrequestFocusInWindowresetKeyboardActionsreshaperevalidatesafelyGetGraphicssafelyGetGraphicsscrollRectToVisiblesetActionMapsetAlignmentXsetAlignmentYsetAutoscrollssetBackgroundsetBordersetComponentPopupMenusetCreatedDoubleBuffersetDebugGraphicsOptionssetDefaultLocalesetDoubleBufferedsetDropLocationsetFocusTraversalKeyssetFontsetForegroundsetInheritsPopupMenusetInputMapsetInputVerifiersetMaximumSizesetMinimumSizesetNextFocusableComponentsetOpaquesetPaintingChildsetPreferredSizesetRequestFocusEnabledsetToolTipTextsetTransferHandlersetUIsetUIPropertysetVerifyInputWhenFocusTargetsetVisiblesetWriteObjCountershouldDebugGraphicssuperProcessMouseMotionEventunregisterKeyboardActionupdate