Class FileChooser
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 Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classDefines an extension filter, used for filtering which files can be chosen in a FileDialog based on the file name extensions. -
Property Summary
PropertiesTypePropertyDescriptionfinal ObjectProperty<File> The initial directory for the displayed file dialog.final ObjectProperty<String> The initial file name for the displayed dialog.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.final StringPropertyThe title of the displayed file dialog. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionGets the extension filters used in the displayed file dialog.final FileGets the value of theinitialDirectoryproperty.final StringGets the value of theinitialFileNameproperty.Gets the value of theselectedExtensionFilterproperty.final StringgetTitle()Gets the value of thetitleproperty.final ObjectProperty<File> The initial directory for the displayed file dialog.final ObjectProperty<String> The initial file name for the displayed dialog.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.final voidsetInitialDirectory(File value) Sets the value of theinitialDirectoryproperty.final voidsetInitialFileName(String value) Sets the value of theinitialFileNameproperty.final voidSets the value of theselectedExtensionFilterproperty.final voidSets the value of thetitleproperty.showOpenDialog(Window ownerWindow) Shows a new file open dialog.showOpenMultipleDialog(Window ownerWindow) Shows a new file open dialog in which multiple files can be selected.showSaveDialog(Window ownerWindow) Shows a new file save dialog.final StringPropertyThe title of the displayed file dialog.
-
Property Details
-
title
-
initialDirectory
The initial directory for the displayed file dialog.- See Also:
-
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
- See Also:
-
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
- See Also:
-
-
Constructor Details
-
FileChooser
public FileChooser()Creates aFileChooser.
-
-
Method Details
-
setTitle
Sets the value of thetitleproperty.- Property description:
- The title of the displayed file dialog.
- Parameters:
value- the value for thetitleproperty- See Also:
-
getTitle
Gets the value of thetitleproperty.- Property description:
- The title of the displayed file dialog.
- Returns:
- the value of the
titleproperty - See Also:
-
titleProperty
The title of the displayed file dialog.- Returns:
- the
titleproperty - See Also:
-
setInitialDirectory
Sets the value of theinitialDirectoryproperty.- Property description:
- The initial directory for the displayed file dialog.
- Parameters:
value- the value for theinitialDirectoryproperty- See Also:
-
getInitialDirectory
Gets the value of theinitialDirectoryproperty.- Property description:
- The initial directory for the displayed file dialog.
- Returns:
- the value of the
initialDirectoryproperty - See Also:
-
initialDirectoryProperty
The initial directory for the displayed file dialog.- Returns:
- the
initialDirectoryproperty - See Also:
-
setInitialFileName
Sets the value of theinitialFileNameproperty.- Property description:
- 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.
- Parameters:
value- the value for theinitialFileNameproperty- Since:
- JavaFX 2.2.40
- See Also:
-
getInitialFileName
Gets the value of theinitialFileNameproperty.- Property description:
- 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.
- Returns:
- the value of the
initialFileNameproperty - Since:
- JavaFX 2.2.40
- See Also:
-
initialFileNameProperty
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.
- Returns:
- the
initialFileNameproperty - Since:
- JavaFX 2.2.40
- See Also:
-
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:
- An observable list of the extension filters used in this dialog
-
selectedExtensionFilterProperty
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.
- Returns:
- the
selectedExtensionFilterproperty - Since:
- JavaFX 8.0
- See Also:
-
setSelectedExtensionFilter
Sets the value of theselectedExtensionFilterproperty.- Property description:
- 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.
- Parameters:
filter- the value for theselectedExtensionFilterproperty- Since:
- JavaFX 8.0
- See Also:
-
getSelectedExtensionFilter
Gets the value of theselectedExtensionFilterproperty.- Property description:
- 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.
- Returns:
- the value of the
selectedExtensionFilterproperty - Since:
- JavaFX 8.0
- See Also:
-
showOpenDialog
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 ornullif 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- the owner window of the displayed file dialog- Returns:
- the selected file or
nullif no file has been selected
-
showOpenMultipleDialog
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 ornullif 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
UnsupportedOperationExceptionon each modification attempt.- Parameters:
ownerWindow- the owner window of the displayed file dialog- Returns:
- the selected files or
nullif no file has been selected
-
showSaveDialog
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 ornullif 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- the owner window of the displayed file dialog- Returns:
- the selected file or
nullif no file has been selected
-