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);
}
| Modifier and Type | Class and Description |
|---|---|
| public static class | FileChooser.
Defines an extension filter, used for filtering which files can be chosen in a FileDialog based on the file name extensions. |
| Modifier and Type | Field and Description |
|---|---|
| private ObservableList | extensionFilters
Specifies the extension filters used in the displayed file dialog. |
| private ObjectProperty | initialDirectory
The initial directory for the displayed file dialog. |
| private ObjectProperty | initialFileName
The initial file name for the displayed dialog. |
| private ObjectProperty | 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. |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| private FileChooser. | |
| public ObservableList | Returns: An observable list of the extension filters used in this dialogGets the extension filters used in the displayed file dialog. |
| public final File | |
| public final String | |
| public final FileChooser. | |
| public final String | |
| public final ObjectProperty | |
| public final ObjectProperty | |
| public final ObjectProperty | |
| public final void | |
| public final void | |
| public final void | |
| public final void | |
| private List | |
| public File | Returns: the selected file ornull if no file has been selectedthe owner window of the displayed file dialog ownerWindow)Shows a new file open dialog. |
| public List | Returns: the selected files ornull if no file has been selectedthe 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 ornull if no file has been selectedthe owner window of the displayed file dialog ownerWindow)Shows a new file save dialog. |
| public final StringProperty |
| extensionFilters | back to summary |
|---|---|
| private ObservableList<FileChooser. Specifies the extension filters used in the displayed file dialog. | |
| initialDirectory | back to summary |
|---|---|
| private ObjectProperty<File> initialDirectory The initial directory for the displayed file dialog. If this path is not set, does not exist, is not a directory, or is not accessible, the dialog will fall back to the platform-default location. The default folder may vary across operating systems. | |
| initialFileName | back 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.
| |
| selectedExtensionFilter | back to summary |
|---|---|
| private ObjectProperty<FileChooser. 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.
| |
| title | back to summary |
|---|---|
| private StringProperty title The title of the displayed file dialog. | |
| FileChooser | back to summary |
|---|---|
| public FileChooser() Creates a | |
| findSelectedFilter | back to summary |
|---|---|
| private FileChooser. | |
| getExtensionFilters | back to summary |
|---|---|
| public ObservableList 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.
| |
| getInitialDirectory | back to summary |
|---|---|
| public final File getInitialDirectory() | |
| getInitialFileName | back to summary |
|---|---|
| public final String getInitialFileName() | |
| getSelectedExtensionFilter | back to summary |
|---|---|
| public final FileChooser. | |
| getTitle | back to summary |
|---|---|
| public final String getTitle() | |
| initialDirectoryProperty | back to summary |
|---|---|
| public final ObjectProperty | |
| initialFileNameProperty | back to summary |
|---|---|
| public final ObjectProperty | |
| selectedExtensionFilterProperty | back to summary |
|---|---|
| public final ObjectProperty | |
| setInitialDirectory | back to summary |
|---|---|
| public final void setInitialDirectory(final File value) | |
| setInitialFileName | back to summary |
|---|---|
| public final void setInitialFileName(final String value) | |
| setSelectedExtensionFilter | back to summary |
|---|---|
| public final void setSelectedExtensionFilter(FileChooser. | |
| setTitle | back to summary |
|---|---|
| public final void setTitle(final String value) | |
| showDialog | back to summary |
|---|---|
| private List | |
| showOpenDialog | back 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 | |
| showOpenMultipleDialog | back to summary |
|---|---|
| public List 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
The returned list is unmodifiable and will throw
| |
| showSaveDialog | back 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 | |
| titleProperty | back to summary |
|---|---|
| public final StringProperty titleProperty() | |
| Modifier and Type | Field and Description |
|---|---|
| private final String | |
| private final List |
| Access | Constructor and Description |
|---|---|
| public | ExtensionFilter(final String
the textual description for the filter description, final String... the accepted file name extensions extensions)Creates an |
| public | ExtensionFilter(final String
the textual description for the filter description, final List<String> the accepted file name extensions extensions)Creates an |
| Modifier and Type | Method and Description |
|---|---|
| public String | |
| public List | Returns: the file name extensionsGets the file name extensions for this |
| private static void |
| description | back to summary |
|---|---|
| private final String description | |
| extensions | back to summary |
|---|---|
| private final List<String> extensions | |
| ExtensionFilter | back to summary |
|---|---|
| public ExtensionFilter(final String description, final String... extensions) Creates an
File name extension should be specified in the
| |
| ExtensionFilter | back to summary |
|---|---|
| public ExtensionFilter(final String description, final List<String> extensions) Creates an
File name extension should be specified in the
| |
| getDescription | back to summary |
|---|---|
| public String getDescription() Gets the description for this
| |
| getExtensions | back to summary |
|---|---|
| public List Gets the file name extensions for this
The returned list is unmodifiable and will throw
| |
| validateArgs | back to summary |
|---|---|
| private static void validateArgs(final String description, final List<String> extensions) | |