Top Description Inners Fields Constructors Methods
javafx.stage

public final Class FileChooser

extends Object
Class Inheritance
Imports
java.io.File, java.util.Collections, .List, .Objects, javafx.beans.property.ObjectProperty, .SimpleObjectProperty, .SimpleStringProperty, .StringProperty, javafx.collections.FXCollections, .ObservableList, com.sun.glass.ui.CommonDialogs, .CommonDialogs.FileChooserResult, com.sun.javafx.tk.FileChooserType, .Toolkit

Provides support for standard platform file dialogs. These dialogs have look and feel of the platform UI components which is independent of JavaFX.

On some platforms where file access may be restricted or not part of the user model (for example, on some mobile or embedded devices), opening a file dialog may always result in a no-op (that is, null file(s) being returned).

A FileChooser can be used to invoke file open dialogs for selecting single file (showOpenDialog), file open dialogs for selecting multiple files (showOpenMultipleDialog) and file save dialogs (showSaveDialog). The configuration of the displayed dialog is controlled by the values of the FileChooser properties set before the corresponding show*Dialog method is called. This configuration includes the dialog's title, the initial directory displayed in the dialog and the extension filter(s) for the listed files. For configuration properties which values haven't been set explicitly, the displayed dialog uses their platform default values. A call to a show dialog method is blocked until the user makes a choice or cancels the dialog. The return value specifies the selected file(s) or equals to null if the dialog has been canceled.

Example:

FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
fileChooser.getExtensionFilters().addAll(
        new ExtensionFilter("Text Files", "*.txt"),
        new ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif"),
        new ExtensionFilter("Audio Files", "*.wav", "*.mp3", "*.aac"),
        new ExtensionFilter("All Files", "*.*"));
File selectedFile = fileChooser.showOpenDialog(mainStage);
if (selectedFile != null) {
   mainStage.display(selectedFile);
}
Since
JavaFX 2.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
FileChooser.ExtensionFilter

Defines an extension filter, used for filtering which files can be chosen in a FileDialog based on the file name extensions.

Field Summary

Modifier and TypeField and Description
private ObservableList<FileChooser.ExtensionFilter>
extensionFilters

Specifies the extension filters used in the displayed file dialog.

private ObjectProperty<File>
initialDirectory

The initial directory for the displayed file dialog.

private ObjectProperty<String>
initialFileName

The initial file name for the displayed dialog.

private ObjectProperty<FileChooser.ExtensionFilter>
selectedExtensionFilter

This property is used to pre-select the extension filter for the next displayed dialog and to read the user-selected extension filter from the dismissed dialog.

private StringProperty
title

The title of the displayed file dialog.

Constructor Summary

AccessConstructor and Description
public
FileChooser()

Creates a FileChooser.

Method Summary

Modifier and TypeMethod and Description
private FileChooser.ExtensionFilter
public ObservableList<FileChooser.ExtensionFilter>

Returns:

An observable list of the extension filters used in this dialog
getExtensionFilters
()

Gets the extension filters used in the displayed file dialog.

public final File
public final String
public final FileChooser.ExtensionFilter
public final String
public final ObjectProperty<File>
public final ObjectProperty<String>
public final ObjectProperty<FileChooser.ExtensionFilter>
public final void
public final void
public final void
public final void
setTitle(final String value)

private List<File>
showDialog(final Window ownerWindow, final FileChooserType fileChooserType)

public File

Returns:

the selected file or null if no file has been selected
showOpenDialog
(final Window
the owner window of the displayed file dialog
ownerWindow
)

Shows a new file open dialog.

public List<File>

Returns:

the selected files or null if no file has been selected
showOpenMultipleDialog
(final Window
the owner window of the displayed file dialog
ownerWindow
)

Shows a new file open dialog in which multiple files can be selected.

public File

Returns:

the selected file or null if no file has been selected
showSaveDialog
(final Window
the owner window of the displayed file dialog
ownerWindow
)

Shows a new file save dialog.

public final StringProperty
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

extensionFiltersback to summary
private ObservableList<FileChooser.ExtensionFilter> extensionFilters

Specifies the extension filters used in the displayed file dialog.

initialDirectoryback to summary
private ObjectProperty<File> initialDirectory

The initial directory for the displayed file dialog.

initialFileNameback to summary
private ObjectProperty<String> initialFileName

The initial file name for the displayed dialog.

This property is used mostly in the displayed file save dialogs as the initial file name for the file being saved. If set for a file open dialog it will have any impact on the displayed dialog only if the corresponding platform provides support for such property in its file open dialogs.

Since
JavaFX 2.2.40
selectedExtensionFilterback to summary
private ObjectProperty<FileChooser.ExtensionFilter> selectedExtensionFilter

This property is used to pre-select the extension filter for the next displayed dialog and to read the user-selected extension filter from the dismissed dialog.

When the file dialog is shown, the selectedExtensionFilter will be checked. If the value of selectedExtensionFilter is null or is not contained in the list of extension filters, then the first extension filter in the list of extension filters will be selected instead. Otherwise, the specified selectedExtensionFilter will be activated.

After the dialog is dismissed the value of this property is updated to match the user-selected extension filter from the dialog.

Since
JavaFX 8.0
titleback to summary
private StringProperty title

The title of the displayed file dialog.

Constructor Detail

FileChooserback to summary
public FileChooser()

Creates a FileChooser.

Method Detail

findSelectedFilterback to summary
private FileChooser.ExtensionFilter findSelectedFilter(CommonDialogs.ExtensionFilter filter)
getExtensionFiltersback to summary
public ObservableList<FileChooser.ExtensionFilter> getExtensionFilters()

Gets the extension filters used in the displayed file dialog. Only one extension filter from the list is active at any time in the displayed dialog and only files which correspond to this extension filter are shown. The first extension filter from the list is activated when the dialog is invoked. Then the user can switch the active extension filter to any other extension filter from the list and in this way control the set of displayed files.

Returns:ObservableList<FileChooser.ExtensionFilter>

An observable list of the extension filters used in this dialog

getInitialDirectoryback to summary
public final File getInitialDirectory()
getInitialFileNameback to summary
public final String getInitialFileName()
getSelectedExtensionFilterback to summary
public final FileChooser.ExtensionFilter getSelectedExtensionFilter()
getTitleback to summary
public final String getTitle()
initialDirectoryPropertyback to summary
public final ObjectProperty<File> initialDirectoryProperty()
initialFileNamePropertyback to summary
public final ObjectProperty<String> initialFileNameProperty()
selectedExtensionFilterPropertyback to summary
public final ObjectProperty<FileChooser.ExtensionFilter> selectedExtensionFilterProperty()
setInitialDirectoryback to summary
public final void setInitialDirectory(final File value)
setInitialFileNameback to summary
public final void setInitialFileName(final String value)
setSelectedExtensionFilterback to summary
public final void setSelectedExtensionFilter(FileChooser.ExtensionFilter filter)
setTitleback to summary
public final void setTitle(final String value)
showDialogback to summary
private List<File> showDialog(final Window ownerWindow, final FileChooserType fileChooserType)
showOpenDialogback to summary
public File showOpenDialog(final Window ownerWindow)

Shows a new file open dialog. The method doesn't return until the displayed open dialog is dismissed. The return value specifies the file chosen by the user or null if no selection has been made. If the owner window for the file dialog is set, input to all windows in the dialog's owner chain is blocked while the file dialog is being shown.

Parameters
ownerWindow:Window

the owner window of the displayed file dialog

Returns:File

the selected file or null if no file has been selected

showOpenMultipleDialogback to summary
public List<File> showOpenMultipleDialog(final Window ownerWindow)

Shows a new file open dialog in which multiple files can be selected. The method doesn't return until the displayed open dialog is dismissed. The return value specifies the files chosen by the user or null if no selection has been made. If the owner window for the file dialog is set, input to all windows in the dialog's owner chain is blocked while the file dialog is being shown.

The returned list is unmodifiable and will throw UnsupportedOperationException on each modification attempt.

Parameters
ownerWindow:Window

the owner window of the displayed file dialog

Returns:List<File>

the selected files or null if no file has been selected

showSaveDialogback to summary
public File showSaveDialog(final Window ownerWindow)

Shows a new file save dialog. The method doesn't return until the displayed file save dialog is dismissed. The return value specifies the file chosen by the user or null if no selection has been made. If the owner window for the file dialog is set, input to all windows in the dialog's owner chain is blocked while the file dialog is being shown.

Parameters
ownerWindow:Window

the owner window of the displayed file dialog

Returns:File

the selected file or null if no file has been selected

titlePropertyback to summary
public final StringProperty titleProperty()
javafx.stage back to summary

public final Class FileChooser.ExtensionFilter

extends Object
Class Inheritance

Defines an extension filter, used for filtering which files can be chosen in a FileDialog based on the file name extensions.
Since
JavaFX 2.0

Field Summary

Modifier and TypeField and Description
private final String
private final List<String>

Constructor Summary

AccessConstructor and Description
public
ExtensionFilter(final String
the textual description for the filter
description
,
final String...
the accepted file name extensions
extensions
)

Creates an ExtensionFilter with the specified description and the file name extensions.

public
ExtensionFilter(final String
the textual description for the filter
description
,
final List<String>
the accepted file name extensions
extensions
)

Creates an ExtensionFilter with the specified description and the file name extensions.

Method Summary

Modifier and TypeMethod and Description
public String

Returns:

the description
getDescription
()

Gets the description for this ExtensionFilter.

public List<String>

Returns:

the file name extensions
getExtensions
()

Gets the file name extensions for this ExtensionFilter.

private static void
validateArgs(final String description, final List<String> extensions)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

descriptionback to summary
private final String description
extensionsback to summary
private final List<String> extensions

Constructor Detail

ExtensionFilterback to summary
public ExtensionFilter(final String description, final String... extensions)

Creates an ExtensionFilter with the specified description and the file name extensions.

File name extension should be specified in the *.<extension> format.

Parameters
description:String

the textual description for the filter

extensions:String[]

the accepted file name extensions

Exceptions
NullPointerException:
if the description or the extensions are null
IllegalArgumentException:
if the description or the extensions are empty
ExtensionFilterback to summary
public ExtensionFilter(final String description, final List<String> extensions)

Creates an ExtensionFilter with the specified description and the file name extensions.

File name extension should be specified in the *.<extension> format.

Parameters
description:String

the textual description for the filter

extensions:List<String>

the accepted file name extensions

Exceptions
NullPointerException:
if the description or the extensions are null
IllegalArgumentException:
if the description or the extensions are empty

Method Detail

getDescriptionback to summary
public String getDescription()

Gets the description for this ExtensionFilter.

Returns:String

the description

getExtensionsback to summary
public List<String> getExtensions()

Gets the file name extensions for this ExtensionFilter.

The returned list is unmodifiable and will throw UnsupportedOperationException on each modification attempt.

Returns:List<String>

the file name extensions

validateArgsback to summary
private static void validateArgs(final String description, final List<String> extensions)