Class SystemFileChooser

java.lang.Object
com.formdev.flatlaf.util.SystemFileChooser

public class SystemFileChooser extends Object
Gives access to operating system file dialogs.

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 type SAVE_DIALOG is ignored. Operating system file dialogs support folder selection only in "Open" dialogs.
  • JFileChooser.FILES_AND_DIRECTORIES is 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() is true, 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. Use chooser.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_DIRECTORIES to true.
  • If no "current directory" is specified, then JFileChooser always opens the users "Documents" folder (on Windows) or "Home" folder (on macOS and Linux).
    SystemFileChooser does the same when first shown, but then remembers last visited folder (either in memory or in a SystemFileChooser.StateStore) and re-uses that folder when SystemFileChooser is shown again.
Since:
3.7