Class SystemFileChooser
There are some limitations and incompatibilities to JFileChooser because
operating system file dialogs do not offer all features that JFileChooser provides.
On the other hand, operating system file dialogs offer features out of the box
that JFileChooser do not offer (e.g. ask for overwrite on save).
So this class offers only features that are available on all platforms.
The API is (mostly) compatible with JFileChooser.
To use this class in existing code, do a string replace from JFileChooser to SystemFileChooser.
If there are no compile errors, then there is a good chance that it works without further changes.
If there are compile errors, then you're using a feature that SystemFileChooser does not support.
Supported platforms are Windows 10+, macOS 10.14+ and Linux with GTK 3.
JFileChooser is used on unsupported platforms or if GTK 3 is not installed.
SystemFileChooser requires FlatLaf native libraries (usually contained in flatlaf.jar).
If not available or disabled (via FlatSystemProperties.USE_NATIVE_LIBRARY
or FlatSystemProperties.USE_SYSTEM_FILE_CHOOSER), then JFileChooser is used.
To improve user experience, it is recommended to use a state storage
(see setStateStore(StateStore)), so that file dialogs open at previously
visited folder.
Limitations/incompatibilities compared to JFileChooser
- Open File and Select Folder dialogs always warn about not existing files/folders. The operating system shows a warning dialog to inform the user. It is not possible to customize that warning dialog. The file dialog stays open.
- Save File dialog always asks whether an existing file should be overwritten. The operating system shows a question dialog to ask the user whether he wants to overwrite the file or not. If user selects "Yes", the file dialog closes. If user selects "No", the file dialog stays open. It is not possible to customize that question dialog.
- Save File dialog does not support multi-selection.
- For selection mode
DIRECTORIES_ONLY, dialog typeSAVE_DIALOGis ignored. Operating system file dialogs support folder selection only in "Open" dialogs. JFileChooser.FILES_AND_DIRECTORIESis not supported.getSelectedFiles()returns selected file also in single selection mode.JFileChooser.getSelectedFiles()only in multi selection mode.- Only file name extension filters (see
SystemFileChooser.FileNameExtensionFilter) are supported on all platforms. - Pattern filters (see
SystemFileChooser.PatternFilter) are only supported on Windows and Linux, but not on macOS. - If adding choosable file filters and
isAcceptAllFileFilterUsed()istrue, then the All Files filter is placed at the end of the combobox list (as usual in current operating systems) and the first choosable filter is selected by default.JFileChooser, on the other hand, adds All Files filter as first item and selects it by default. Usechooser.addChoosableFileFilter( chooser.getAcceptAllFileFilter() )to place All Files filter somewhere else. - Accessory components are not supported.
- macOS: By default, the user can not navigate into file packages (e.g. applications).
If needed, this can be enabled by setting platform property
MAC_TREATS_FILE_PACKAGES_AS_DIRECTORIEStotrue. - If no "current directory" is specified, then
JFileChooseralways opens the users "Documents" folder (on Windows) or "Home" folder (on macOS and Linux).
SystemFileChooserdoes the same when first shown, but then remembers last visited folder (either in memory or in aSystemFileChooser.StateStore) and re-uses that folder whenSystemFileChooseris shown again.
- Since:
- 3.7
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfacestatic classstatic classstatic final classstatic final classA case-insensitive file filter which accepts file patterns containing the wildcard characters*?on Windows and Linux.static interfaceSimple state storage used to persist file chooser state (e.g. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final intstatic final intstatic final intstatic final StringLinux: Low-level options to clear.static final StringLinux: Low-level options to set.static final StringmacOS: Text displayed in front of the filter combobox.static final StringmacOS: Text displayed at top of open/save dialogs.static final StringmacOS: Text displayed in front of the filename text field in save dialog (not used in open dialog).static final StringmacOS: Low-level options to clear.static final StringmacOS: Low-level options to set.static final StringmacOS: Iftrue, displays file packages (e.g.static final intstatic final intstatic final StringWindows: Default extension to be added to file name in save dialog.static final StringWindows: Folder used as a default if there is not a recently used folder value available.static final StringWindows: Text displayed in front of the filename text field.static final StringWindows: Low-level options to clear.static final StringWindows: Low-level options to set. -
Constructor Summary
ConstructorsConstructorDescriptionSystemFileChooser(File currentDirectory) SystemFileChooser(String currentDirectoryPath) -
Method Summary
Modifier and TypeMethodDescriptionvoidintintintReturns file-selection mode.<T> TFile[]static SystemFileChooser.StateStoreReturns state storage used to persist file chooser state (e.g.Returns the ID used to prefix keys in state storage.booleanbooleanbooleanbooleanbooleanvoidputPlatformProperty(String key, Object value) Set a platform specific file dialog property.booleanvoidvoidsetAcceptAllFileFilterUsed(boolean acceptAll) voidsetApproveButtonMnemonic(char mnemonic) voidsetApproveButtonMnemonic(int mnemonic) voidsetApproveButtonText(String approveButtonText) voidsetApproveCallback(SystemFileChooser.ApproveCallback approveCallback) Sets a callback that is invoked when user presses "OK" button (or double-clicks a file).voidsetCurrentDirectory(File dir) voidsetDialogTitle(String dialogTitle) voidsetDialogType(int dialogType) voidvoidsetFileHidingEnabled(boolean useFileHiding) voidsetFileSelectionMode(int fileSelectionMode) Sets file-selection mode.voidsetMultiSelectionEnabled(boolean multiSelection) voidsetSelectedFile(File file) voidsetSelectedFiles(File[] selectedFiles) static voidsetStateStore(SystemFileChooser.StateStore stateStore) Sets state storage used to persist file chooser state (e.g.voidsetStateStoreID(String stateStoreID) Sets the ID used to prefix keys in state storage.intshowDialog(Component parent, String approveButtonText) intshowOpenDialog(Component parent) intshowSaveDialog(Component parent)
-
Field Details
-
OPEN_DIALOG
public static final int OPEN_DIALOG- See Also:
-
SAVE_DIALOG
public static final int SAVE_DIALOG- See Also:
-
CANCEL_OPTION
public static final int CANCEL_OPTION- See Also:
-
APPROVE_OPTION
public static final int APPROVE_OPTION- See Also:
-
FILES_ONLY
public static final int FILES_ONLY- See Also:
-
DIRECTORIES_ONLY
public static final int DIRECTORIES_ONLY- See Also:
-
WINDOWS_FILE_NAME_LABEL
-
WINDOWS_DEFAULT_FOLDER
Windows: Folder used as a default if there is not a recently used folder value available. Windows somewhere stores default folder on a per-app basis. So this is probably used only once when the app opens a file dialog for first time. Value type must beString.- See Also:
-
WINDOWS_DEFAULT_EXTENSION
-
MAC_MESSAGE
-
MAC_FILTER_FIELD_LABEL
-
MAC_NAME_FIELD_LABEL
-
MAC_TREATS_FILE_PACKAGES_AS_DIRECTORIES
-
WINDOWS_OPTIONS_SET
Windows: Low-level options to set. SeeFOS_*constants inFlatNativeWindowsLibrary. OptionsFOS_PICKFOLDERS,FOS_ALLOWMULTISELECTandFOS_FORCESHOWHIDDENcan not be modified. Value type must beInteger.- See Also:
-
WINDOWS_OPTIONS_CLEAR
Windows: Low-level options to clear. SeeFOS_*constants inFlatNativeWindowsLibrary. OptionsFOS_PICKFOLDERS,FOS_ALLOWMULTISELECTandFOS_FORCESHOWHIDDENcan not be modified. Value type must beInteger.- See Also:
-
MAC_OPTIONS_SET
macOS: Low-level options to set. SeeFC_*constants inFlatNativeMacLibrary. OptionsFC_canChooseFiles,FC_canChooseDirectories,FC_allowsMultipleSelectionandFC_showsHiddenFilescan not be modified. Value type must beInteger.- See Also:
-
MAC_OPTIONS_CLEAR
macOS: Low-level options to clear. SeeFC_*constants inFlatNativeMacLibrary. OptionsFC_canChooseFiles,FC_canChooseDirectories,FC_allowsMultipleSelectionandFC_showsHiddenFilescan not be modified. Value type must beInteger.- See Also:
-
LINUX_OPTIONS_SET
Linux: Low-level options to set. SeeFC_*constants inFlatNativeLinuxLibrary. OptionsFC_select_folder,FC_select_multipleandFC_show_hiddencan not be modified. Value type must beInteger.- See Also:
-
LINUX_OPTIONS_CLEAR
Linux: Low-level options to clear. SeeFC_*constants inFlatNativeLinuxLibrary. OptionsFC_select_folder,FC_select_multipleandFC_show_hiddencan not be modified. Value type must beInteger.- See Also:
-
-
Constructor Details
-
SystemFileChooser
public SystemFileChooser()- See Also:
-
SystemFileChooser
- See Also:
-
SystemFileChooser
- See Also:
-
-
Method Details
-
showOpenDialog
- See Also:
-
showSaveDialog
- See Also:
-
showDialog
-
getDialogType
public int getDialogType()- See Also:
-
setDialogType
public void setDialogType(int dialogType) - See Also:
-
getDialogTitle
- See Also:
-
setDialogTitle
- See Also:
-
getApproveButtonText
- See Also:
-
setApproveButtonText
- See Also:
-
getApproveButtonMnemonic
public int getApproveButtonMnemonic()- See Also:
-
setApproveButtonMnemonic
public void setApproveButtonMnemonic(int mnemonic) - See Also:
-
setApproveButtonMnemonic
public void setApproveButtonMnemonic(char mnemonic) - See Also:
-
getFileSelectionMode
public int getFileSelectionMode()Returns file-selection mode. Possible values areFILES_ONLYandDIRECTORIES_ONLY. Default isFILES_ONLY.- See Also:
-
setFileSelectionMode
public void setFileSelectionMode(int fileSelectionMode) Sets file-selection mode. Possible values areFILES_ONLYandDIRECTORIES_ONLY.- See Also:
-
isFileSelectionEnabled
public boolean isFileSelectionEnabled()- See Also:
-
isDirectorySelectionEnabled
public boolean isDirectorySelectionEnabled()- See Also:
-
isMultiSelectionEnabled
public boolean isMultiSelectionEnabled()- See Also:
-
setMultiSelectionEnabled
public void setMultiSelectionEnabled(boolean multiSelection) - See Also:
-
isFileHidingEnabled
public boolean isFileHidingEnabled()- See Also:
-
setFileHidingEnabled
public void setFileHidingEnabled(boolean useFileHiding) - See Also:
-
getCurrentDirectory
- See Also:
-
setCurrentDirectory
- See Also:
-
getSelectedFile
- See Also:
-
setSelectedFile
- See Also:
-
getSelectedFiles
- See Also:
-
setSelectedFiles
- See Also:
-
getChoosableFileFilters
- See Also:
-
addChoosableFileFilter
- See Also:
-
removeChoosableFileFilter
- See Also:
-
resetChoosableFileFilters
public void resetChoosableFileFilters()- See Also:
-
getAcceptAllFileFilter
- See Also:
-
isAcceptAllFileFilterUsed
public boolean isAcceptAllFileFilterUsed()- See Also:
-
setAcceptAllFileFilterUsed
public void setAcceptAllFileFilterUsed(boolean acceptAll) - See Also:
-
getFileFilter
- See Also:
-
setFileFilter
- See Also:
-
getApproveCallback
-
setApproveCallback
Sets a callback that is invoked when user presses "OK" button (or double-clicks a file). The file dialog is still open. If the callback returnsCANCEL_OPTION, then the file dialog stays open. If it returnsAPPROVE_OPTION(or any value other thanCANCEL_OPTION), the file dialog is closed and theshow...Dialog()methods return that value.The callback has two parameters:
File[] selectedFiles- one or more selected filesApproveContext context- context object that provides additional methods
orchooser.setApproveCallback( (selectedFiles, context) -> { // do something return SystemFileChooser.APPROVE_OPTION; // or SystemFileChooser.CANCEL_OPTION } );
WARNING: Do not show a Swing dialog from within the callback. This will not work!chooser.setApproveCallback( this::approveCallback ); ... private boolean approveCallback( File[] selectedFiles, ApproveContext context ) { // do something return SystemFileChooser.APPROVE_OPTION; // or SystemFileChooser.CANCEL_OPTION }Instead, use
SystemFileChooser.ApproveContext.showMessageDialog(int, String, String, int, String...), which shows a modal system message dialog as child of the file dialog.chooser.setApproveCallback( (selectedFiles, context) -> { if( !selectedFiles[0].getName().startsWith( "blabla" ) ) { context.showMessageDialog( JOptionPane.WARNING_MESSAGE, "File name must start with 'blabla' :)", null, 0 ); return SystemFileChooser.CANCEL_OPTION; } return SystemFileChooser.APPROVE_OPTION; } );- See Also:
-
getPlatformProperty
-
putPlatformProperty
Set a platform specific file dialog property.For supported properties see
WINDOWS_,MAC_andLINUX_constants in this class.chooser.putPlatformProperty( SystemFileChooser.WINDOWS_FILE_NAME_LABEL, "My filename label:" ); chooser.putPlatformProperty( SystemFileChooser.MAC_TREATS_FILE_PACKAGES_AS_DIRECTORIES, true ); chooser.putPlatformProperty( SystemFileChooser.LINUX_OPTIONS_CLEAR, FlatNativeLinuxLibrary.FC_create_folders | FlatNativeLinuxLibrary.FC_do_overwrite_confirmation ); -
getStateStore
Returns state storage used to persist file chooser state (e.g. last used directory). Ornullif there is no state storage (the default). -
setStateStore
Sets state storage used to persist file chooser state (e.g. last used directory). -
getStateStoreID
Returns the ID used to prefix keys in state storage. Ornull(the default). -
setStateStoreID
Sets the ID used to prefix keys in state storage. Ornull(the default).By specifying an ID, an application can have different persisted states for different kinds of file dialogs within the application. E.g. Import/Export file dialogs could use a different ID then Open/Save file dialogs.
-