FileDocCategorySizeDatePackage
JFileChooser.javaAPI DocJava SE 6 API63499Tue Jun 10 00:26:36 BST 2008javax.swing

JFileChooser

public class JFileChooser extends JComponent implements Accessible
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.

beaninfo
attribute: isContainer false description: A component which allows for the interactive selection of a file.
version
1.117 08/08/07
author
Jeff Dinkins

Fields Summary
private static final String
uiClassID
public static final int
OPEN_DIALOG
Type value indicating that the JFileChooser supports an "Open" file operation.
public static final int
SAVE_DIALOG
Type value indicating that the JFileChooser supports a "Save" file operation.
public static final int
CUSTOM_DIALOG
Type value indicating that the JFileChooser supports a developer-specified file operation.
public static final int
CANCEL_OPTION
Return value if cancel is chosen.
public static final int
APPROVE_OPTION
Return value if approve (yes, ok) is chosen.
public static final int
ERROR_OPTION
Return value if an error occured.
public static final int
FILES_ONLY
Instruction to display only files.
public static final int
DIRECTORIES_ONLY
Instruction to display only directories.
public static final int
FILES_AND_DIRECTORIES
Instruction to display both files and directories.
public static final String
CANCEL_SELECTION
Instruction to cancel the current selection.
public static final String
APPROVE_SELECTION
Instruction to approve the current selection (same as pressing yes or ok).
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 String
APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY
Identifies change in the mnemonic for the approve (yes, ok) button.
public static final String
CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY
Instruction to display the control buttons.
public static final String
DIRECTORY_CHANGED_PROPERTY
Identifies user's directory change.
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.
public static final String
MULTI_SELECTION_ENABLED_CHANGED_PROPERTY
Enables multiple-file selections.
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.
public static final String
FILE_HIDING_CHANGED_PROPERTY
Identifies a change in the display-hidden-files property.
public static final String
FILE_FILTER_CHANGED_PROPERTY
User changed the kind of files to display.
public static final String
FILE_SELECTION_MODE_CHANGED_PROPERTY
Identifies a change in the kind of selection (single, multiple, etc.).
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
ACCEPT_ALL_FILE_FILTER_USED_CHANGED_PROPERTY
Identifies whether a the AcceptAllFileFilter is used or not.
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).
public static final String
CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY
Identifies a change in the list of predefined file filters the user can choose from.
private String
dialogTitle
private String
approveButtonText
private String
approveButtonToolTipText
private int
approveButtonMnemonic
private ActionListener
actionListener
private Vector
filters
private JDialog
dialog
private int
dialogType
private int
returnValue
private JComponent
accessory
private FileView
fileView
private transient FileView
uiFileView
private boolean
controlsShown
private boolean
useFileHiding
private static final String
SHOW_HIDDEN_PROP
private transient PropertyChangeListener
showFilesListener
private int
fileSelectionMode
private boolean
multiSelectionEnabled
private boolean
useAcceptAllFileFilter
private boolean
dragEnabled
private FileFilter
fileFilter
private FileSystemView
fileSystemView
private File
currentDirectory
private File
selectedFile
private File[]
selectedFiles
protected AccessibleContext
accessibleContext
Constructors Summary
public JFileChooser()
Constructs a 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.


    // *************************************
    // ***** JFileChooser Constructors *****
    // *************************************

                                         
      
	this((File) null, (FileSystemView) null);
    
public JFileChooser(String currentDirectoryPath)
Constructs a 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.

param
currentDirectoryPath a String giving the path to a file or directory

	this(currentDirectoryPath, (FileSystemView) null);
    
public JFileChooser(File currentDirectory)
Constructs a 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.

param
currentDirectory a File object specifying the path to a file or directory

	this(currentDirectory, (FileSystemView) null);
    
public JFileChooser(FileSystemView fsv)
Constructs a JFileChooser using the given FileSystemView.

	this((File) null, fsv);
    
public JFileChooser(File currentDirectory, FileSystemView fsv)
Constructs a JFileChooser using the given current directory and FileSystemView.

	setup(fsv);
	setCurrentDirectory(currentDirectory);
    
public JFileChooser(String currentDirectoryPath, FileSystemView fsv)
Constructs a JFileChooser using the given current directory path and FileSystemView.

	setup(fsv);
	if(currentDirectoryPath == null) {
	    setCurrentDirectory(null);
        } else {
	    setCurrentDirectory(fileSystemView.createFileObject(currentDirectoryPath));
	}
    
Methods Summary
public booleanaccept(java.io.File f)
Returns true if the file should be displayed.

param
f the File
return
true if the file should be displayed, otherwise false
see
FileFilter#accept

	boolean shown = true;
	if(f != null && fileFilter != null) {
	    shown = fileFilter.accept(f);
	}
	return shown;
    
public voidaddActionListener(java.awt.event.ActionListener l)
Adds an ActionListener to the file chooser.

param
l the listener to be added
see
#approveSelection
see
#cancelSelection

        listenerList.add(ActionListener.class, l);
    
public voidaddChoosableFileFilter(javax.swing.filechooser.FileFilter filter)
Adds a filter to the list of user choosable file filters. For information on setting the file selection mode, see {@link #setFileSelectionMode setFileSelectionMode}.

param
filter the FileFilter to add to the choosable file filter list
beaninfo
preferred: true bound: true description: Adds a filter to the list of user choosable file filters.
see
#getChoosableFileFilters
see
#removeChoosableFileFilter
see
#resetChoosableFileFilters
see
#setFileSelectionMode

	if(filter != null && !filters.contains(filter)) {
	    FileFilter[] oldValue = getChoosableFileFilters();
	    filters.addElement(filter);
	    firePropertyChange(CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY, oldValue, getChoosableFileFilters());
	} 
	setFileFilter(filter);
    
public voidapproveSelection()
Called by the UI when the user hits the Approve button (labeled "Open" or "Save", by default). This can also be called by the programmer. This method causes an action event to fire with the command string equal to APPROVE_SELECTION.

see
#APPROVE_SELECTION

	returnValue = APPROVE_OPTION;
	if(dialog != null) {
	    dialog.setVisible(false);
	}
	fireActionPerformed(APPROVE_SELECTION);
    
public voidcancelSelection()
Called by the UI when the user chooses the Cancel button. This can also be called by the programmer. This method causes an action event to fire with the command string equal to CANCEL_SELECTION.

see
#CANCEL_SELECTION

	returnValue = CANCEL_OPTION;
	if(dialog != null) {
	    dialog.setVisible(false);
	}
	fireActionPerformed(CANCEL_SELECTION);
    
public voidchangeToParentDirectory()
Changes the directory to be set to the parent of the current directory.

see
#getCurrentDirectory

	selectedFile = null;
	File oldValue = getCurrentDirectory();
	setCurrentDirectory(getFileSystemView().getParentDirectory(oldValue));
    
protected javax.swing.JDialogcreateDialog(java.awt.Component parent)
Creates and returns a new 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;
}
}

param
parent the parent component of the dialog; can be null
return
a new JDialog containing this instance
exception
HeadlessException if GraphicsEnvironment.isHeadless() returns true.
see
java.awt.GraphicsEnvironment#isHeadless
since
1.4

	String title = getUI().getDialogTitle(this);
        putClientProperty(AccessibleContext.ACCESSIBLE_DESCRIPTION_PROPERTY, 
                          title);

        JDialog dialog;
        Window window = JOptionPane.getWindowForComponent(parent);
        if (window instanceof Frame) {
            dialog = new JDialog((Frame)window, title, true);	
        } else {
            dialog = new JDialog((Dialog)window, title, true);
        }
        dialog.setComponentOrientation(this.getComponentOrientation());

        Container contentPane = dialog.getContentPane();
        contentPane.setLayout(new BorderLayout());
        contentPane.add(this, BorderLayout.CENTER);
 
        if (JDialog.isDefaultLookAndFeelDecorated()) {
            boolean supportsWindowDecorations = 
            UIManager.getLookAndFeel().getSupportsWindowDecorations();
            if (supportsWindowDecorations) {
                dialog.getRootPane().setWindowDecorationStyle(JRootPane.FILE_CHOOSER_DIALOG);
            }
        }
        dialog.pack();
        dialog.setLocationRelativeTo(parent);

	return dialog;
    
public voidensureFileIsVisible(java.io.File f)
Makes sure that the specified file is viewable, and not hidden.

param
f a File object

        getUI().ensureFileIsVisible(this, f);
    
protected voidfireActionPerformed(java.lang.String command)
Notifies all listeners that have registered interest for notification on this event type. The event instance is lazily created using the command parameter.

see
EventListenerList

        // Guaranteed to return a non-null array
        Object[] listeners = listenerList.getListenerList();
        long mostRecentEventTime = EventQueue.getMostRecentEventTime();
        int modifiers = 0;
        AWTEvent currentEvent = EventQueue.getCurrentEvent();
        if (currentEvent instanceof InputEvent) {
            modifiers = ((InputEvent)currentEvent).getModifiers();
        } else if (currentEvent instanceof ActionEvent) {
            modifiers = ((ActionEvent)currentEvent).getModifiers();
        }
        ActionEvent e = null;
        // Process the listeners last to first, notifying
        // those that are interested in this event
        for (int i = listeners.length-2; i>=0; i-=2) {
            if (listeners[i]==ActionListener.class) {
                // Lazily create the event:
                if (e == null) {
                    e = new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
                                        command, mostRecentEventTime,
                                        modifiers);
                }
                ((ActionListener)listeners[i+1]).actionPerformed(e);
            }
        }
    
public javax.swing.filechooser.FileFiltergetAcceptAllFileFilter()
Returns the AcceptAll file filter. For example, on Microsoft Windows this would be All Files (*.*).

	FileFilter filter = null;
	if(getUI() != null) {
	    filter = getUI().getAcceptAllFileFilter(this);
	}
	return filter;
    
public javax.accessibility.AccessibleContextgetAccessibleContext()
Gets the AccessibleContext associated with this JFileChooser. For file choosers, the AccessibleContext takes the form of an AccessibleJFileChooser. A new AccessibleJFileChooser instance is created if necessary.

return
an AccessibleJFileChooser that serves as the AccessibleContext of this JFileChooser


                                                          
       
        if (accessibleContext == null) {
            accessibleContext = new AccessibleJFileChooser();
        }
        return accessibleContext;
    
public javax.swing.JComponentgetAccessory()
Returns the accessory component.

return
this JFileChooser's accessory component, or null
see
#setAccessory

        return accessory;
    
public java.awt.event.ActionListener[]getActionListeners()
Returns an array of all the action listeners registered on this file chooser.

return
all of this file chooser's ActionListeners or an empty array if no action listeners are currently registered
see
#addActionListener
see
#removeActionListener
since
1.4

        return (ActionListener[])listenerList.getListeners(
                ActionListener.class);
    
public intgetApproveButtonMnemonic()
Returns the approve button's mnemonic.

return
an integer value for the mnemonic key
see
#setApproveButtonMnemonic

	return approveButtonMnemonic;
    
public java.lang.StringgetApproveButtonText()
Returns the text used in the ApproveButton in the FileChooserUI. If null, the UI object will determine the button's text. Typically, this would be "Open" or "Save".

return
the text used in the ApproveButton
see
#setApproveButtonText
see
#setDialogType
see
#showDialog

	return approveButtonText;
    
public java.lang.StringgetApproveButtonToolTipText()
Returns the tooltip text used in the ApproveButton. If null, the UI object will determine the button's text.

return
the tooltip text used for the approve button
see
#setApproveButtonText
see
#setDialogType
see
#showDialog

	return approveButtonToolTipText;
    
public javax.swing.filechooser.FileFilter[]getChoosableFileFilters()
Gets the list of user choosable file filters.

return
a FileFilter array containing all the choosable file filters
see
#addChoosableFileFilter
see
#removeChoosableFileFilter
see
#resetChoosableFileFilters

	FileFilter[] filterArray = new FileFilter[filters.size()];
	filters.copyInto(filterArray);
	return filterArray;
    
public booleangetControlButtonsAreShown()
Returns the value of the controlButtonsAreShown property.

return
the value of the controlButtonsAreShown property
see
#setControlButtonsAreShown
since
1.3

	return controlsShown;
    
public java.io.FilegetCurrentDirectory()
Returns the current directory.

return
the current directory
see
#setCurrentDirectory

	return currentDirectory;
    
public java.lang.StringgetDescription(java.io.File f)
Returns the file description.

param
f the File
return
the String containing the file description for f
see
FileView#getDescription

	String description = null;
	if(f != null) {
	    if(getFileView() != null) {
		description = getFileView().getDescription(f);
	    }
	    if(description == null && uiFileView != null) {
		description = uiFileView.getDescription(f);
	    }
        }
	return description;
    
public java.lang.StringgetDialogTitle()
Gets the string that goes in the JFileChooser's titlebar.

see
#setDialogTitle

	return dialogTitle;
    
public intgetDialogType()
Returns the type of this dialog. The default is JFileChooser.OPEN_DIALOG.

return
the type of dialog to be displayed:
  • JFileChooser.OPEN_DIALOG
  • JFileChooser.SAVE_DIALOG
  • JFileChooser.CUSTOM_DIALOG
see
#setDialogType

	return dialogType;
    
public booleangetDragEnabled()
Gets the value of the dragEnabled property.

return
the value of the dragEnabled property
see
#setDragEnabled
since
1.4

	return dragEnabled;
    
public javax.swing.filechooser.FileFiltergetFileFilter()
Returns the currently selected file filter.

return
the current file filter
see
#setFileFilter
see
#addChoosableFileFilter

	return fileFilter;
    
public intgetFileSelectionMode()
Returns the current file-selection mode. The default is JFilesChooser.FILES_ONLY.

return
the type of files to be displayed, one of the following:
  • JFileChooser.FILES_ONLY
  • JFileChooser.DIRECTORIES_ONLY
  • JFileChooser.FILES_AND_DIRECTORIES
see
#setFileSelectionMode

	return fileSelectionMode;
    
public javax.swing.filechooser.FileSystemViewgetFileSystemView()
Returns the file system view.

return
the FileSystemView object
see
#setFileSystemView

	return fileSystemView;
    
public javax.swing.filechooser.FileViewgetFileView()
Returns the current file view.

see
#setFileView

	return fileView;
    
public javax.swing.IcongetIcon(java.io.File f)
Returns the icon for this file or type of file, depending on the system.

param
f the File
return
the Icon for this file, or type of file
see
FileView#getIcon

	Icon icon = null;
	if (f != null) {
	    if(getFileView() != null) {
		icon = getFileView().getIcon(f);
	    }
	    if(icon == null && uiFileView != null) {
		icon = uiFileView.getIcon(f);
	    }
	}
	return icon;
    
public java.lang.StringgetName(java.io.File f)
Returns the filename.

param
f the File
return
the String containing the filename for f
see
FileView#getName

	String filename = null;
	if(f != null) {
	    if(getFileView() != null) {
		filename = getFileView().getName(f);
	    }
	    if(filename == null && uiFileView != null) {
		filename = uiFileView.getName(f);
	    }
        }
	return filename;
    
public java.io.FilegetSelectedFile()
Returns the selected file. This can be set either by the programmer via 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.

see
#setSelectedFile
return
the selected file

	return selectedFile;
    
public java.io.File[]getSelectedFiles()
Returns a list of selected files if the file chooser is set to allow multiple selection.

	if(selectedFiles == null) {
	    return new File[0];
	} else {
	    return (File[]) selectedFiles.clone();
	}
    
public java.lang.StringgetTypeDescription(java.io.File f)
Returns the file type.

param
f the File
return
the String containing the file type description for f
see
FileView#getTypeDescription

	String typeDescription = null;
	if(f != null) {
	    if(getFileView() != null) {
		typeDescription = getFileView().getTypeDescription(f);
	    }
	    if(typeDescription == null && uiFileView != null) {
		typeDescription = uiFileView.getTypeDescription(f);
	    }
        }
	return typeDescription;
    
public javax.swing.plaf.FileChooserUIgetUI()
Gets the UI object which implements the L&F for this component.

return
the FileChooserUI object that implements the FileChooserUI L&F

        return (FileChooserUI) ui;
    
public java.lang.StringgetUIClassID()
Returns a string that specifies the name of the L&F class that renders this component.

return
the string "FileChooserUI"
see
JComponent#getUIClassID
see
UIDefaults#getUI
beaninfo
expert: true description: A string that specifies the name of the L&F class.

        return uiClassID;
    
private voidinstallShowFilesListener()

        // Track native setting for showing hidden files
        Toolkit tk = Toolkit.getDefaultToolkit();
        Object showHiddenProperty = tk.getDesktopProperty(SHOW_HIDDEN_PROP);
        if (showHiddenProperty instanceof Boolean) {
            useFileHiding = !((Boolean)showHiddenProperty).booleanValue();
            showFilesListener = new WeakPCL(this);
            tk.addPropertyChangeListener(SHOW_HIDDEN_PROP, showFilesListener);
        }
    
public booleanisAcceptAllFileFilterUsed()
Returns whether the AcceptAll FileFilter is used.

return
true if the AcceptAll FileFilter is used
see
#setAcceptAllFileFilterUsed
since
1.3

	return useAcceptAllFileFilter;
    
public booleanisDirectorySelectionEnabled()
Convenience call that determines if directories are selectable based on the current file selection mode.

see
#setFileSelectionMode
see
#getFileSelectionMode

	return ((fileSelectionMode == DIRECTORIES_ONLY) || (fileSelectionMode == FILES_AND_DIRECTORIES));
    
public booleanisFileHidingEnabled()
Returns true if hidden files are not shown in the file chooser; otherwise, returns false.

return
the status of the file hiding property
see
#setFileHidingEnabled

	return useFileHiding;
    
public booleanisFileSelectionEnabled()
Convenience call that determines if files are selectable based on the current file selection mode.

see
#setFileSelectionMode
see
#getFileSelectionMode

	return ((fileSelectionMode == FILES_ONLY) || (fileSelectionMode == FILES_AND_DIRECTORIES));
    
public booleanisMultiSelectionEnabled()
Returns true if multiple files can be selected.

return
true if multiple files can be selected
see
#setMultiSelectionEnabled

	return multiSelectionEnabled;
    
public booleanisTraversable(java.io.File f)
Returns true if the file (directory) can be visited. Returns false if the directory cannot be traversed.

param
f the File
return
true if the file/directory can be traversed, otherwise false
see
FileView#isTraversable

	Boolean traversable = null;
	if (f != null) {
	    if (getFileView() != null) {
		traversable = getFileView().isTraversable(f);
	    }
	    if (traversable == null && uiFileView != null) {
		traversable = uiFileView.isTraversable(f);
	    }
	    if (traversable == null) {
		traversable = getFileSystemView().isTraversable(f);
	    }
	}
	return (traversable != null && traversable.booleanValue());
    
protected java.lang.StringparamString()
Returns a string representation of this 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.

return
a string representation of this JFileChooser

        String approveButtonTextString = (approveButtonText != null ?
					  approveButtonText: "");
        String dialogTitleString = (dialogTitle != null ?
				    dialogTitle: "");
        String dialogTypeString;
        if (dialogType == OPEN_DIALOG) {
            dialogTypeString = "OPEN_DIALOG";
        } else if (dialogType == SAVE_DIALOG) {
            dialogTypeString = "SAVE_DIALOG";
        } else if (dialogType == CUSTOM_DIALOG) {
            dialogTypeString = "CUSTOM_DIALOG";
        } else dialogTypeString = "";
        String returnValueString;
        if (returnValue == CANCEL_OPTION) {
            returnValueString = "CANCEL_OPTION";
        } else if (returnValue == APPROVE_OPTION) {
            returnValueString = "APPROVE_OPTION";
        } else if (returnValue == ERROR_OPTION) {
            returnValueString = "ERROR_OPTION";
        } else returnValueString = "";
        String useFileHidingString = (useFileHiding ?
                                    "true" : "false");
        String fileSelectionModeString;
        if (fileSelectionMode == FILES_ONLY) {
            fileSelectionModeString = "FILES_ONLY";
        } else if (fileSelectionMode == DIRECTORIES_ONLY) {
            fileSelectionModeString = "DIRECTORIES_ONLY";
        } else if (fileSelectionMode == FILES_AND_DIRECTORIES) {
            fileSelectionModeString = "FILES_AND_DIRECTORIES";
        } else fileSelectionModeString = "";
        String currentDirectoryString = (currentDirectory != null ?
					 currentDirectory.toString() : "");
        String selectedFileString = (selectedFile != null ?
				     selectedFile.toString() : "");

        return super.paramString() +
        ",approveButtonText=" + approveButtonTextString +
        ",currentDirectory=" + currentDirectoryString +
        ",dialogTitle=" + dialogTitleString +
        ",dialogType=" + dialogTypeString +
        ",fileSelectionMode=" + fileSelectionModeString +
        ",returnValue=" + returnValueString +
        ",selectedFile=" + selectedFileString +
        ",useFileHiding=" + useFileHidingString;
    
private voidreadObject(java.io.ObjectInputStream in)
See readObject and writeObject in JComponent for more information about serialization in Swing.

        in.defaultReadObject();
        installShowFilesListener();
    
public voidremoveActionListener(java.awt.event.ActionListener l)
Removes an ActionListener from the file chooser.

param
l the listener to be removed
see
#addActionListener

        listenerList.remove(ActionListener.class, l);
    
public booleanremoveChoosableFileFilter(javax.swing.filechooser.FileFilter f)
Removes a filter from the list of user choosable file filters. Returns true if the file filter was removed.

see
#addChoosableFileFilter
see
#getChoosableFileFilters
see
#resetChoosableFileFilters

	if(filters.contains(f)) {
            if(getFileFilter() == f) {
		setFileFilter(null);
            }
	    FileFilter[] oldValue = getChoosableFileFilters();
	    filters.removeElement(f);
	    firePropertyChange(CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY, oldValue, getChoosableFileFilters());
	    return true;
	} else {
	    return false;
	}
    
public voidrescanCurrentDirectory()
Tells the UI to rescan its files list from the current directory.

        getUI().rescanCurrentDirectory(this);
    
public voidresetChoosableFileFilters()
Resets the choosable file filter list to its starting state. Normally, this removes all added file filters while leaving the AcceptAll file filter.

see
#addChoosableFileFilter
see
#getChoosableFileFilters
see
#removeChoosableFileFilter

	FileFilter[] oldValue = getChoosableFileFilters();
	setFileFilter(null);
	filters.removeAllElements();
	if(isAcceptAllFileFilterUsed()) {
	   addChoosableFileFilter(getAcceptAllFileFilter());
	}
	firePropertyChange(CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY, oldValue, getChoosableFileFilters());
    
public voidsetAcceptAllFileFilterUsed(boolean b)
Determines whether the 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.

beaninfo
preferred: true bound: true description: Sets whether the AcceptAll FileFilter is used as an available choice in the choosable filter list.
see
#isAcceptAllFileFilterUsed
see
#getAcceptAllFileFilter
see
#setFileFilter
since
1.3

	boolean oldValue = useAcceptAllFileFilter;
	useAcceptAllFileFilter = b;
	if(!b) {
	    removeChoosableFileFilter(getAcceptAllFileFilter());
	} else {
	    removeChoosableFileFilter(getAcceptAllFileFilter());
	    addChoosableFileFilter(getAcceptAllFileFilter());
	}
	firePropertyChange(ACCEPT_ALL_FILE_FILTER_USED_CHANGED_PROPERTY, oldValue, useAcceptAllFileFilter);
    
public voidsetAccessory(javax.swing.JComponent newAccessory)
Sets the accessory component. An accessory is often used to show a preview image of the selected file; however, it can be used for anything that the programmer wishes, such as extra custom file chooser controls.

Note: if there was a previous accessory, you should unregister any listeners that the accessory might have registered with the file chooser.

beaninfo
preferred: true bound: true description: Sets the accessory component on the JFileChooser.

        JComponent oldValue = accessory;
        accessory = newAccessory;
	firePropertyChange(ACCESSORY_CHANGED_PROPERTY, oldValue, accessory);
    
public voidsetApproveButtonMnemonic(int mnemonic)
Sets the approve button's mnemonic using a numeric keycode.

param
mnemonic an integer value for the mnemonic key
beaninfo
preferred: true bound: true description: The mnemonic key accelerator for the ApproveButton.
see
#getApproveButtonMnemonic

	if(approveButtonMnemonic == mnemonic) {
	   return;
	}
	int oldValue = approveButtonMnemonic;
	approveButtonMnemonic = mnemonic;
	firePropertyChange(APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY, oldValue, approveButtonMnemonic);
    
public voidsetApproveButtonMnemonic(char mnemonic)
Sets the approve button's mnemonic using a character.

param
mnemonic a character value for the mnemonic key
see
#getApproveButtonMnemonic

        int vk = (int) mnemonic;
        if(vk >= 'a" && vk <='z") {
	    vk -= ('a" - 'A");
	}
        setApproveButtonMnemonic(vk);
    
public voidsetApproveButtonText(java.lang.String approveButtonText)
Sets the text used in the ApproveButton in the FileChooserUI.

beaninfo
preferred: true bound: true description: The text that goes in the ApproveButton.
param
approveButtonText the text used in the ApproveButton
see
#getApproveButtonText
see
#setDialogType
see
#showDialog

	if(this.approveButtonText == approveButtonText) {
	    return;
	}
	String oldValue = this.approveButtonText;
	this.approveButtonText = approveButtonText;
	firePropertyChange(APPROVE_BUTTON_TEXT_CHANGED_PROPERTY, oldValue, approveButtonText);
    
public voidsetApproveButtonToolTipText(java.lang.String toolTipText)
Sets the tooltip text used in the ApproveButton. If null, the UI object will determine the button's text.

beaninfo
preferred: true bound: true description: The tooltip text for the ApproveButton.
param
toolTipText the tooltip text for the approve button
see
#setApproveButtonText
see
#setDialogType
see
#showDialog

	if(approveButtonToolTipText == toolTipText) {
	    return;
	}
	String oldValue = approveButtonToolTipText;
	approveButtonToolTipText = toolTipText;
	firePropertyChange(APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY, oldValue, approveButtonToolTipText);
    
public voidsetControlButtonsAreShown(boolean b)
Sets the property that indicates whether the approve and cancel buttons are shown in the file chooser. This property is 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.

param
b false if control buttons should not be shown; otherwise, true
beaninfo
preferred: true bound: true description: Sets whether the approve & cancel buttons are shown.
see
#getControlButtonsAreShown
see
#CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY
since
1.3

	if(controlsShown == b) {
	    return;
	}
	boolean oldValue = controlsShown;
	controlsShown = b;
	firePropertyChange(CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY, oldValue, controlsShown);
    
public voidsetCurrentDirectory(java.io.File dir)
Sets the current directory. Passing in 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.

beaninfo
preferred: true bound: true description: The directory that the JFileChooser is showing files of.
param
dir the current directory to point to
see
#getCurrentDirectory

	File oldValue = currentDirectory;
	
	if (dir != null && !dir.exists()) {
	    dir = currentDirectory;
	}
	if (dir == null) {
	    dir = getFileSystemView().getDefaultDirectory();
	}
	if (currentDirectory != null) {
	    /* Verify the toString of object */
	    if (this.currentDirectory.equals(dir)) {
		return;
	    }
	}
	
	File prev = null;
	while (!isTraversable(dir) && prev != dir) {
	    prev = dir;
	    dir = getFileSystemView().getParentDirectory(dir);
	}
	currentDirectory = dir;

	firePropertyChange(DIRECTORY_CHANGED_PROPERTY, oldValue, currentDirectory);
    
public voidsetDialogTitle(java.lang.String dialogTitle)
Sets the string that goes in the JFileChooser window's title bar.

param
dialogTitle the new String for the title bar
beaninfo
preferred: true bound: true description: The title of the JFileChooser dialog window.
see
#getDialogTitle

	String oldValue = this.dialogTitle;
	this.dialogTitle = dialogTitle;
	if(dialog != null) {
	    dialog.setTitle(dialogTitle);
	}
	firePropertyChange(DIALOG_TITLE_CHANGED_PROPERTY, oldValue, dialogTitle);
    
public voidsetDialogType(int dialogType)
Sets the type of this dialog. Use 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.

param
dialogType the type of dialog to be displayed:
  • JFileChooser.OPEN_DIALOG
  • JFileChooser.SAVE_DIALOG
  • JFileChooser.CUSTOM_DIALOG
exception
IllegalArgumentException if dialogType is not legal
beaninfo
preferred: true bound: true description: The type (open, save, custom) of the JFileChooser. enum: OPEN_DIALOG JFileChooser.OPEN_DIALOG SAVE_DIALOG JFileChooser.SAVE_DIALOG CUSTOM_DIALOG JFileChooser.CUSTOM_DIALOG
see
#getDialogType
see
#setApproveButtonText

	if(this.dialogType == dialogType) {
	    return;
	}
	if(!(dialogType == OPEN_DIALOG || dialogType == SAVE_DIALOG || dialogType == CUSTOM_DIALOG)) {
	    throw new IllegalArgumentException("Incorrect Dialog Type: " + dialogType);
	}
	int oldValue = this.dialogType;
	this.dialogType = dialogType;
	if(dialogType == OPEN_DIALOG || dialogType == SAVE_DIALOG) {
	    setApproveButtonText(null);
	}
	firePropertyChange(DIALOG_TYPE_CHANGED_PROPERTY, oldValue, dialogType);
    
public voidsetDragEnabled(boolean b)
Sets the 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 an item 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.

param
b the value to set the dragEnabled property to
exception
HeadlessException if b is true and GraphicsEnvironment.isHeadless() returns true
see
java.awt.GraphicsEnvironment#isHeadless
see
#getDragEnabled
see
#setTransferHandler
see
TransferHandler
since
1.4
beaninfo
description: determines whether automatic drag handling is enabled bound: false

        if (b && GraphicsEnvironment.isHeadless()) {
            throw new HeadlessException();
        }
	dragEnabled = b;
    
public voidsetFileFilter(javax.swing.filechooser.FileFilter filter)
Sets the current file filter. The file filter is used by the file chooser to filter out files from the user's view.

beaninfo
preferred: true bound: true description: Sets the File Filter used to filter out files of type.
param
filter the new current file filter to use
see
#getFileFilter

	FileFilter oldValue = fileFilter;
	fileFilter = filter;
	if (filter != null) {
	    if (isMultiSelectionEnabled() && selectedFiles != null && selectedFiles.length > 0) {
		Vector fList = new Vector();
		boolean failed = false;
		for (int i = 0; i < selectedFiles.length; i++) {
		    if (filter.accept(selectedFiles[i])) {
			fList.add(selectedFiles[i]);
		    } else {
			failed = true;
		    }
		}
		if (failed) {
		    setSelectedFiles((fList.size() == 0) ? null : (File[])fList.toArray(new File[fList.size()]));
		}
	    } else if (selectedFile != null && !filter.accept(selectedFile)) {
		setSelectedFile(null);
	    }
	}
	firePropertyChange(FILE_FILTER_CHANGED_PROPERTY, oldValue, fileFilter);
    
public voidsetFileHidingEnabled(boolean b)
Sets file hiding on or off. If true, hidden files are not shown in the file chooser. The job of determining which files are shown is done by the FileView.

beaninfo
preferred: true bound: true description: Sets file hiding on or off.
param
b the boolean value that determines whether file hiding is turned on
see
#isFileHidingEnabled

        // Dump showFilesListener since we'll ignore it from now on
        if (showFilesListener != null) {
            Toolkit.getDefaultToolkit().removePropertyChangeListener(SHOW_HIDDEN_PROP, showFilesListener);
            showFilesListener = null;
        } 
        boolean oldValue = useFileHiding;
        useFileHiding = b;
        firePropertyChange(FILE_HIDING_CHANGED_PROPERTY, oldValue, useFileHiding);
    
public voidsetFileSelectionMode(int mode)
Sets the JFileChooser to allow the user to just select files, just select directories, or select both files and directories. The default is JFilesChooser.FILES_ONLY.

param
mode the type of files to be displayed:
  • JFileChooser.FILES_ONLY
  • JFileChooser.DIRECTORIES_ONLY
  • JFileChooser.FILES_AND_DIRECTORIES
exception
IllegalArgumentException if mode is an illegal file selection mode
beaninfo
preferred: true bound: true description: Sets the types of files that the JFileChooser can choose. enum: FILES_ONLY JFileChooser.FILES_ONLY DIRECTORIES_ONLY JFileChooser.DIRECTORIES_ONLY FILES_AND_DIRECTORIES JFileChooser.FILES_AND_DIRECTORIES
see
#getFileSelectionMode

	if(fileSelectionMode == mode) {
	    return;
	}

        if ((mode == FILES_ONLY) || (mode == DIRECTORIES_ONLY) || (mode == FILES_AND_DIRECTORIES)) {
	   int oldValue = fileSelectionMode;
	   fileSelectionMode = mode;
	   firePropertyChange(FILE_SELECTION_MODE_CHANGED_PROPERTY, oldValue, fileSelectionMode);
        } else {
	   throw new IllegalArgumentException("Incorrect Mode for file selection: " + mode);
        }
    
public voidsetFileSystemView(javax.swing.filechooser.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.

param
fsv the new FileSystemView
beaninfo
expert: true bound: true description: Sets the FileSytemView used to get filesystem information.
see
FileSystemView

	FileSystemView oldValue = fileSystemView;
	fileSystemView = fsv;
	firePropertyChange(FILE_SYSTEM_VIEW_CHANGED_PROPERTY, oldValue, fileSystemView);
    
public voidsetFileView(javax.swing.filechooser.FileView fileView)
Sets the file view to used to retrieve UI information, such as the icon that represents a file or the type description of a file.

beaninfo
preferred: true bound: true description: Sets the File View used to get file type information.
see
#getFileView

	FileView oldValue = this.fileView;
	this.fileView = fileView;
	firePropertyChange(FILE_VIEW_CHANGED_PROPERTY, oldValue, fileView);
    
public voidsetMultiSelectionEnabled(boolean b)
Sets the file chooser to allow multiple file selections.

param
b true if multiple files may be selected
beaninfo
bound: true description: Sets multiple file selection mode.
see
#isMultiSelectionEnabled

	if(multiSelectionEnabled == b) {
	    return;
	}
	boolean oldValue = multiSelectionEnabled;
	multiSelectionEnabled = b;
	firePropertyChange(MULTI_SELECTION_ENABLED_CHANGED_PROPERTY, oldValue, multiSelectionEnabled);
    
public voidsetSelectedFile(java.io.File file)
Sets the selected file. If the file's parent directory is not the current directory, changes the current directory to be the file's parent directory.

beaninfo
preferred: true bound: true
see
#getSelectedFile
param
file the selected file

	File oldValue = selectedFile;
	selectedFile = file;
	if(selectedFile != null) {
	    if (file.isAbsolute() && !getFileSystemView().isParent(getCurrentDirectory(), selectedFile)) {
		setCurrentDirectory(selectedFile.getParentFile());
	    }
	    if (!isMultiSelectionEnabled() || selectedFiles == null || selectedFiles.length == 1) {
		ensureFileIsVisible(selectedFile);
	    }
	}
	firePropertyChange(SELECTED_FILE_CHANGED_PROPERTY, oldValue, selectedFile);
    
public voidsetSelectedFiles(java.io.File[] selectedFiles)
Sets the list of selected files if the file chooser is set to allow multiple selection.

beaninfo
bound: true description: The list of selected files if the chooser is in multiple selection mode.

	File[] oldValue = this.selectedFiles;
	if (selectedFiles != null && selectedFiles.length == 0) {
	    selectedFiles = null;
	}
	this.selectedFiles = selectedFiles;
	setSelectedFile((selectedFiles != null) ? selectedFiles[0] : null);
	firePropertyChange(SELECTED_FILES_CHANGED_PROPERTY, oldValue, this.selectedFiles);
    
protected voidsetup(javax.swing.filechooser.FileSystemView view)
Performs common constructor initialization and setup.

        installShowFilesListener();

        if(view == null) {
            view = FileSystemView.getFileSystemView();
        }
        setFileSystemView(view);
        updateUI(); 
        if(isAcceptAllFileFilterUsed()) {
            setFileFilter(getAcceptAllFileFilter());
        }
        enableEvents(AWTEvent.MOUSE_EVENT_MASK);
    
public intshowDialog(java.awt.Component parent, java.lang.String approveButtonText)
Pops a custom file chooser dialog with a custom approve button. For example, the following code pops up a file chooser with a "Run Application" button (instead of the normal "Save" or "Open" button):
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.

param
parent the parent component of the dialog; can be null
param
approveButtonText the text of the ApproveButton
return
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
exception
HeadlessException if GraphicsEnvironment.isHeadless() returns true.
see
java.awt.GraphicsEnvironment#isHeadless

	if(approveButtonText != null) {
	    setApproveButtonText(approveButtonText);
	    setDialogType(CUSTOM_DIALOG);
	}
	dialog = createDialog(parent);
	dialog.addWindowListener(new WindowAdapter() {
	    public void windowClosing(WindowEvent e) {
		returnValue = CANCEL_OPTION;
	    }
	});
	returnValue = ERROR_OPTION;
	rescanCurrentDirectory();

	dialog.show();
        firePropertyChange("JFileChooserDialogIsClosingProperty", dialog, null);
	dialog.removeAll();
	dialog.dispose();
	dialog = null;
	return returnValue;
    
public intshowOpenDialog(java.awt.Component parent)
Pops up an "Open File" file chooser dialog. Note that the text that appears in the approve button is determined by the L&F.

param
parent the parent component of the dialog, can be null; see showDialog for details
return
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
exception
HeadlessException if GraphicsEnvironment.isHeadless() returns true.
see
java.awt.GraphicsEnvironment#isHeadless
see
#showDialog

	setDialogType(OPEN_DIALOG);
	return showDialog(parent, null);
    
public intshowSaveDialog(java.awt.Component parent)
Pops up a "Save File" file chooser dialog. Note that the text that appears in the approve button is determined by the L&F.

param
parent the parent component of the dialog, can be null; see showDialog for details
return
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
exception
HeadlessException if GraphicsEnvironment.isHeadless() returns true.
see
java.awt.GraphicsEnvironment#isHeadless
see
#showDialog

	setDialogType(SAVE_DIALOG);
	return showDialog(parent, null);
    
public voidupdateUI()
Resets the UI property to a value from the current look and feel.

see
JComponent#updateUI

	if (isAcceptAllFileFilterUsed()) {
	    removeChoosableFileFilter(getAcceptAllFileFilter());
	}
	FileChooserUI ui = ((FileChooserUI)UIManager.getUI(this));
	if (fileSystemView == null) {
	    // We were probably deserialized
	    setFileSystemView(FileSystemView.getFileSystemView());
	}
	setUI(ui);

	uiFileView = getUI().getFileView(this);
	if(isAcceptAllFileFilterUsed()) {
	    addChoosableFileFilter(getAcceptAllFileFilter());
	}
    
private voidwriteObject(java.io.ObjectOutputStream s)
See readObject and writeObject in JComponent for more information about serialization in Swing.

	FileSystemView fsv = null;

	if (isAcceptAllFileFilterUsed()) {
	    //The AcceptAllFileFilter is UI specific, it will be reset by
	    //updateUI() after deserialization
	    removeChoosableFileFilter(getAcceptAllFileFilter());
	}
	if (fileSystemView.equals(FileSystemView.getFileSystemView())) {
	    //The default FileSystemView is platform specific, it will be
	    //reset by updateUI() after deserialization
	    fsv = fileSystemView;
	    fileSystemView = null;
	}
        s.defaultWriteObject();
	if (fsv != null) {
	    fileSystemView = fsv;
	}
	if (isAcceptAllFileFilterUsed()) {
	    addChoosableFileFilter(getAcceptAllFileFilter());
	}
        if (getUIClassID().equals(uiClassID)) {
            byte count = JComponent.getWriteObjCounter(this);
            JComponent.setWriteObjCounter(this, --count);
            if (count == 0 && ui != null) {
                ui.installUI(this);
            }
        }