Module qupath.fx

Class FileChoosers

java.lang.Object
qupath.fx.dialogs.FileChoosers

public class FileChoosers extends Object
Helper class for creating and displaying JavaFX file choosers.

This assists when working with file and directory choosers in several ways:

  • Static methods and builders are provided to reduce boilerplate.
  • It is possible to call the prompts from any thread, and the chooser will be displayed on the JavaFX application thread.
  • When using the main static prompt methods with a specified owner, the last directory used is retained for each owner window.
  • When using save file choosers with extension filters that contain multiple parts (e.g. "*.tar.gz") then the returned file is checked and updated if the parts are not all included. This is intended to overcome a problem when the returned file may only contain the last part of the extension.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Abstract base class for builders for JavaFX FileChooser and DirectoryChooser.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static javafx.stage.FileChooser.ExtensionFilter
    Extension filter that accepts all files.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static FileChoosers.Builder<javafx.stage.DirectoryChooser>
    Create a builder to build a customized JavaFX DirectoryChooser.
    static FileChoosers.Builder<javafx.stage.FileChooser>
    Create a builder to build a customized JavaFX FileChooser.
    static javafx.stage.FileChooser.ExtensionFilter
    createExtensionFilter(String description, String... extensions)
    Convenience method to create a FileChooser.ExtensionFilter instance from a description and array of extensions.
    static javafx.stage.FileChooser.ExtensionFilter
    createExtensionFilter(String description, Collection<String> extensions)
    Convenience method to create a FileChooser.ExtensionFilter instance from a description and collection of extensions.
    static void
    main(String[] args)
    Method for interactive testing, that shows a save dialog with a multi-extension filter.
    static File
    Show a directory chooser that prompts the user to select a single directory, using a default title and initial directory.
    static File
    promptForDirectory(String title, File initialDir)
    Show a directory chooser that prompts the user to select a single directory.
    static File
    promptForDirectory(javafx.stage.Window owner, String title, File initialDir)
    Show a directory chooser that prompts the user to select a single directory, with an optional parent window and title.
    static File
    promptForFile(String title, javafx.stage.FileChooser.ExtensionFilter... extensionFilters)
    Prompt the user to a select a file.
    static File
    promptForFile(javafx.stage.FileChooser.ExtensionFilter... extensionFilters)
    Prompt the user to a select a file, using the default title for an 'Open file' dialog.
    static File
    promptForFile(javafx.stage.Window owner, String title, javafx.stage.FileChooser.ExtensionFilter... extensionFilters)
    Prompt the user to a select a file, with an optional parent window and title.
    static String
    promptForFilePathOrURI(String title, String defaultText, javafx.stage.FileChooser.ExtensionFilter... extensionFilters)
    Prompt the user to select a file path or URI.
    static String
    promptForFilePathOrURI(javafx.stage.FileChooser.ExtensionFilter... extensionFilters)
    Prompt the user to select a file path or URI, using a default title.
    static String
    promptForFilePathOrURI(javafx.stage.Window owner, String title, String defaultText, javafx.stage.FileChooser.ExtensionFilter... extensionFilters)
    Prompt the user to select a file path or URI, with an optional owner window and title.
    static List<File>
    promptForMultipleFiles(String title, javafx.stage.FileChooser.ExtensionFilter... extensionFilters)
    Show a file chooser that prompts the user to select multiple files.
    static List<File>
    promptForMultipleFiles(javafx.stage.FileChooser.ExtensionFilter... extensionFilters)
    Show a file chooser that prompts the user to select multiple files, using a default title.
    static List<File>
    promptForMultipleFiles(javafx.stage.Window owner, String title, javafx.stage.FileChooser.ExtensionFilter... extensionFilters)
    Show a file chooser that prompts the user to select multiple files, with an optional parent window and title.
    static File
    promptToSaveFile(String title, File initialFile, javafx.stage.FileChooser.ExtensionFilter... extensionFilters)
    Prompt user to select a file to save, with an optional parent window and title.
    static File
    promptToSaveFile(javafx.stage.FileChooser.ExtensionFilter... extensionFilters)
    Prompt user to select a file to save, using the default title.
    static File
    promptToSaveFile(javafx.stage.Window owner, String title, File initialFile, javafx.stage.FileChooser.ExtensionFilter... extensionFilters)
    Prompt user to select a file to save, with an optional owner window and title.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • FILTER_ALL_FILES

      public static javafx.stage.FileChooser.ExtensionFilter FILTER_ALL_FILES
      Extension filter that accepts all files.
  • Constructor Details

    • FileChoosers

      public FileChoosers()
  • Method Details

    • promptForMultipleFiles

      public static List<File> promptForMultipleFiles(javafx.stage.FileChooser.ExtensionFilter... extensionFilters)
      Show a file chooser that prompts the user to select multiple files, using a default title.
      Parameters:
      extensionFilters - optional file extension filters
      Returns:
      a list of files selected by the user, or null if the chooser was cancelled
    • promptForMultipleFiles

      public static List<File> promptForMultipleFiles(String title, javafx.stage.FileChooser.ExtensionFilter... extensionFilters)
      Show a file chooser that prompts the user to select multiple files.
      Parameters:
      title - title for the file chooser (not supported on all platforms, may be null)
      extensionFilters - optional file extension filters
      Returns:
      a list of files selected by the user, or null if the chooser was cancelled
    • promptForMultipleFiles

      public static List<File> promptForMultipleFiles(javafx.stage.Window owner, String title, javafx.stage.FileChooser.ExtensionFilter... extensionFilters)
      Show a file chooser that prompts the user to select multiple files, with an optional parent window and title.
      Parameters:
      owner - window that owns the file chooser
      title - title for the file chooser (not supported on all platforms, may be null)
      extensionFilters - optional file extension filters
      Returns:
      a list of files selected by the user, or null if the chooser was cancelled
    • promptForDirectory

      public static File promptForDirectory()
      Show a directory chooser that prompts the user to select a single directory, using a default title and initial directory.
      Returns:
    • promptForDirectory

      public static File promptForDirectory(String title, File initialDir)
      Show a directory chooser that prompts the user to select a single directory.
      Parameters:
      title - title for the directory chooser (not supported on all platforms, may be null)
      initialDir - initial directory to display (optional, may be null)
      Returns:
      selected directory, or null if no directory was selected
    • promptForDirectory

      public static File promptForDirectory(javafx.stage.Window owner, String title, File initialDir)
      Show a directory chooser that prompts the user to select a single directory, with an optional parent window and title.
      Parameters:
      owner - window that owns the file chooser
      title - title for the directory chooser (not supported on all platforms, may be null)
      initialDir - initial directory to display (optional, may be null)
      Returns:
      a list of files selected by the user, or null if the chooser was cancelled
    • promptForFile

      public static File promptForFile(javafx.stage.FileChooser.ExtensionFilter... extensionFilters)
      Prompt the user to a select a file, using the default title for an 'Open file' dialog.
      Parameters:
      extensionFilters - optional file extension filters
      Returns:
      the File selected by the user, or null if the dialog was cancelled
    • promptForFile

      public static File promptForFile(String title, javafx.stage.FileChooser.ExtensionFilter... extensionFilters)
      Prompt the user to a select a file.
      Parameters:
      title - the title to display for the dialog (may be null to use default)
      extensionFilters - optional file extension filters
      Returns:
      the File selected by the user, or null if the dialog was cancelled
    • promptForFile

      public static File promptForFile(javafx.stage.Window owner, String title, javafx.stage.FileChooser.ExtensionFilter... extensionFilters)
      Prompt the user to a select a file, with an optional parent window and title.
      Parameters:
      owner - window that owns the file chooser
      title - title for the file chooser (not supported on all platforms, may be null)
      extensionFilters - optional file extension filters
      Returns:
      a list of files selected by the user, or null if the chooser was cancelled
    • promptToSaveFile

      public static File promptToSaveFile(javafx.stage.FileChooser.ExtensionFilter... extensionFilters)
      Prompt user to select a file to save, using the default title.
      Parameters:
      extensionFilters - optional file extension filters
      Returns:
      the file selected by the user, or null if the chooser was cancelled
    • promptToSaveFile

      public static File promptToSaveFile(String title, File initialFile, javafx.stage.FileChooser.ExtensionFilter... extensionFilters)
      Prompt user to select a file to save, with an optional parent window and title.
      Parameters:
      title - title for the file chooser (not supported on all platforms, may be null)
      initialFile - the initial file or directory (optional, may be null)
      extensionFilters - optional file extension filters
      Returns:
      the file selected by the user, or null if the chooser was cancelled
    • promptToSaveFile

      public static File promptToSaveFile(javafx.stage.Window owner, String title, File initialFile, javafx.stage.FileChooser.ExtensionFilter... extensionFilters)
      Prompt user to select a file to save, with an optional owner window and title.
      Parameters:
      owner - window that owns the file chooser
      title - title for the file chooser (not supported on all platforms, may be null)
      initialFile - the initial file or directory (optional, may be null)
      extensionFilters - optional file extension filters
      Returns:
      the File selected by the user, or null if the dialog was cancelled
    • promptForFilePathOrURI

      public static String promptForFilePathOrURI(javafx.stage.FileChooser.ExtensionFilter... extensionFilters)
      Prompt the user to select a file path or URI, using a default title. This provides a text field for the user to enter a URI, and a button to open a file chooser.
      Parameters:
      extensionFilters - optional file extension filters for the chooser
      Returns:
      the contents of the text field from the dialog, or null if the dialog was cancelled. Note that these may have been populated by the chooser, in which case an absolute file path is returned. Alternatively, the user may have entered a URI. Because the text field can be freely edited, there is no guarantee that the returned string will represent either a valid file path or URI.
    • promptForFilePathOrURI

      public static String promptForFilePathOrURI(String title, String defaultText, javafx.stage.FileChooser.ExtensionFilter... extensionFilters)
      Prompt the user to select a file path or URI. This provides a text field for the user to enter a URI, and a button to open a file chooser.
      Parameters:
      title - dialog title
      defaultText - default file path or URI to display (may be null)
      extensionFilters - optional file extension filters for the chooser
      Returns:
      the contents of the text field from the dialog, or null if the dialog was cancelled. Note that these may have been populated by the chooser, in which case an absolute file path is returned. Alternatively, the user may have entered a URI. Because the text field can be freely edited, there is no guarantee that the returned string will represent either a valid file path or URI.
    • promptForFilePathOrURI

      public static String promptForFilePathOrURI(javafx.stage.Window owner, String title, String defaultText, javafx.stage.FileChooser.ExtensionFilter... extensionFilters)
      Prompt the user to select a file path or URI, with an optional owner window and title. This provides a text field for the user to enter a URI, and a button to open a file chooser.
      Parameters:
      owner - window that owns the file chooser
      title - dialog title
      defaultText - default file path or URI to display (may be null)
      extensionFilters - optional file extension filters for the chooser
      Returns:
      the contents of the text field from the dialog, or null if the dialog was cancelled. Note that these may have been populated by the chooser, in which case an absolute file path is returned. Alternatively, the user may have entered a URI. Because the text field can be freely edited, there is no guarantee that the returned string will represent either a valid file path or URI.
    • createExtensionFilter

      public static javafx.stage.FileChooser.ExtensionFilter createExtensionFilter(String description, String... extensions)
      Convenience method to create a FileChooser.ExtensionFilter instance from a description and array of extensions. This checks to ensure that the provided extensions start with ".*", and appends one or both characters if necessary.
      Parameters:
      description - description of the filter
      extensions - file extensions associated with the filter
      Returns:
    • createExtensionFilter

      public static javafx.stage.FileChooser.ExtensionFilter createExtensionFilter(String description, Collection<String> extensions)
      Convenience method to create a FileChooser.ExtensionFilter instance from a description and collection of extensions. This checks to ensure that the provided extensions start with ".*", and appends one or both characters if necessary.
      Parameters:
      description -
      extensions -
      Returns:
    • buildFileChooser

      public static FileChoosers.Builder<javafx.stage.FileChooser> buildFileChooser()
      Create a builder to build a customized JavaFX FileChooser.
      Returns:
    • buildDirectoryChooser

      public static FileChoosers.Builder<javafx.stage.DirectoryChooser> buildDirectoryChooser()
      Create a builder to build a customized JavaFX DirectoryChooser.
      Returns:
    • main

      public static void main(String[] args)
      Method for interactive testing, that shows a save dialog with a multi-extension filter.
      Parameters:
      args -