public final class FileChooser
extends java.lang.Object
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);
}
Type | Property and Description |
---|---|
<any> |
initialDirectory
The initial directory for the displayed file dialog.
|
<any> |
initialFileName
The initial file name for the displayed dialog.
|
<any> |
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.
|
StringProperty |
title
The title of the displayed file dialog.
|
Modifier and Type | Class and Description |
---|---|
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.
|
Modifier and Type | Field and Description |
---|---|
private <any> |
extensionFilters
Specifies the extension filters used in the displayed file dialog.
|
private <any> |
initialDirectory
The initial directory for the displayed file dialog.
|
private <any> |
initialFileName
The initial file name for the displayed dialog.
|
private <any> |
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 and Description |
---|
FileChooser() |
Modifier and Type | Method and Description |
---|---|
private FileChooser.ExtensionFilter |
findSelectedFilter(CommonDialogs.ExtensionFilter filter) |
<any> |
getExtensionFilters()
Gets the extension filters used in the displayed file dialog.
|
java.io.File |
getInitialDirectory()
Gets the value of the property initialDirectory.
|
java.lang.String |
getInitialFileName()
Gets the value of the property initialFileName.
|
FileChooser.ExtensionFilter |
getSelectedExtensionFilter()
Gets the value of the property selectedExtensionFilter.
|
java.lang.String |
getTitle()
Gets the value of the property title.
|
<any> |
initialDirectoryProperty()
The initial directory for the displayed file dialog.
|
<any> |
initialFileNameProperty()
The initial file name for the displayed dialog.
|
<any> |
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.
|
void |
setInitialDirectory(java.io.File value)
Sets the value of the property initialDirectory.
|
void |
setInitialFileName(java.lang.String value)
Sets the value of the property initialFileName.
|
void |
setSelectedExtensionFilter(FileChooser.ExtensionFilter filter)
Sets the value of the property selectedExtensionFilter.
|
void |
setTitle(java.lang.String value)
Sets the value of the property title.
|
private java.util.List<java.io.File> |
showDialog(Window ownerWindow,
FileChooserType fileChooserType) |
java.io.File |
showOpenDialog(Window ownerWindow)
Shows a new file open dialog.
|
java.util.List<java.io.File> |
showOpenMultipleDialog(Window ownerWindow)
Shows a new file open dialog in which multiple files can be selected.
|
java.io.File |
showSaveDialog(Window ownerWindow)
Shows a new file save dialog.
|
StringProperty |
titleProperty()
The title of the displayed file dialog.
|
public final StringProperty titleProperty
getTitle()
,
setTitle(String)
public final <any> initialDirectoryProperty
getInitialDirectory()
,
setInitialDirectory(File)
public final <any> initialFileNameProperty
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.
getInitialFileName()
,
setInitialFileName(String)
public final <any> selectedExtensionFilterProperty
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.
getSelectedExtensionFilter()
,
setSelectedExtensionFilter(ExtensionFilter)
private StringProperty title
private <any> initialDirectory
private <any> initialFileName
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.
private <any> extensionFilters
private <any> selectedExtensionFilter
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.
public final void setTitle(java.lang.String value)
public final java.lang.String getTitle()
public final StringProperty titleProperty()
getTitle()
,
setTitle(String)
public final void setInitialDirectory(java.io.File value)
public final java.io.File getInitialDirectory()
public final <any> initialDirectoryProperty()
getInitialDirectory()
,
setInitialDirectory(File)
public final void setInitialFileName(java.lang.String value)
public final java.lang.String getInitialFileName()
public final <any> initialFileNameProperty()
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.
getInitialFileName()
,
setInitialFileName(String)
public <any> getExtensionFilters()
public final <any> selectedExtensionFilterProperty()
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.
getSelectedExtensionFilter()
,
setSelectedExtensionFilter(ExtensionFilter)
public final void setSelectedExtensionFilter(FileChooser.ExtensionFilter filter)
public final FileChooser.ExtensionFilter getSelectedExtensionFilter()
public java.io.File showOpenDialog(Window ownerWindow)
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.ownerWindow
- the owner window of the displayed file dialognull
if no file has been selectedpublic java.util.List<java.io.File> showOpenMultipleDialog(Window ownerWindow)
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.
ownerWindow
- the owner window of the displayed file dialognull
if no file has been selectedpublic java.io.File showSaveDialog(Window ownerWindow)
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.ownerWindow
- the owner window of the displayed file dialognull
if no file has been selectedprivate FileChooser.ExtensionFilter findSelectedFilter(CommonDialogs.ExtensionFilter filter)
private java.util.List<java.io.File> showDialog(Window ownerWindow, FileChooserType fileChooserType)