Class RecentFiles

java.lang.Object
com.dlsc.gemsfx.util.RecentFiles

public class RecentFiles extends Object
Manages a "Recent Files" list, persisting file paths via Java Preferences and providing a self-updating JavaFX Menu that reflects the current list.

Typical usage:

  RecentFiles recentFiles = new RecentFiles(
      Preferences.userNodeForPackage(MyApp.class));
  recentFiles.setOnOpenFile(file -> loadDocument(file));
  menuBar.getMenus().add(recentFiles.getMenu());

  // After the user opens a file:
  recentFiles.add(chosenFile);

The default maximum number of entries is 5. Use setMaxFiles(int) to change it.

  • Property Summary

    Properties
    Type
    Property
    Description
    final javafx.beans.property.ObjectProperty<Consumer<File>>
    The callback invoked when the user clicks a recent file entry in the menu.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Default maximum number of recent file entries shown in the menu.
    static final String
    Default preferences key under which file paths are stored.
  • Constructor Summary

    Constructors
    Constructor
    Description
    RecentFiles(Preferences preferences)
    Creates a RecentFiles manager using the given Preferences node.
    RecentFiles(Preferences preferences, String prefsKey)
    Creates a RecentFiles manager using the given Preferences node and a custom preferences key.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(File file)
    Adds the given file to the top of the recent files list and persists it immediately.
    void
    Clears the recent files list and persists the change immediately.
    final int
    Returns the maximum number of recent files tracked and shown in the menu.
    javafx.scene.control.Menu
    Returns the Menu that reflects the current recent files list.
    final Consumer<File>
    Gets the value of the onOpenFile property.
    javafx.collections.ObservableList<File>
    Returns an unmodifiable observable list of the current recent files.
    final javafx.beans.property.ObjectProperty<Consumer<File>>
    The callback invoked when the user clicks a recent file entry in the menu.
    final void
    setMaxFiles(int max)
    Sets the maximum number of recent files tracked and shown in the menu.
    final void
    Sets the value of the onOpenFile property.

    Methods inherited from class Object

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

  • Field Details

    • DEFAULT_MAX_FILES

      public static final int DEFAULT_MAX_FILES
      Default maximum number of recent file entries shown in the menu.
      See Also:
    • DEFAULT_PREFS_KEY

      public static final String DEFAULT_PREFS_KEY
      Default preferences key under which file paths are stored.
      See Also:
  • Constructor Details

    • RecentFiles

      public RecentFiles(Preferences preferences)
      Creates a RecentFiles manager using the given Preferences node. File paths are stored under the key "recent.files".
      Parameters:
      preferences - the preferences node used for persistence; must not be null
    • RecentFiles

      public RecentFiles(Preferences preferences, String prefsKey)
      Creates a RecentFiles manager using the given Preferences node and a custom preferences key.
      Parameters:
      preferences - the preferences node used for persistence; must not be null
      prefsKey - the key under which file paths are stored; must not be null
  • Method Details

    • add

      public void add(File file)
      Adds the given file to the top of the recent files list and persists it immediately. Duplicate entries are automatically removed before re-inserting at the top.
      Parameters:
      file - the file to add; ignored if null
    • clear

      public void clear()
      Clears the recent files list and persists the change immediately.
    • getMenu

      public javafx.scene.control.Menu getMenu()
      Returns the Menu that reflects the current recent files list. Add this to a MenuBar. The menu updates automatically whenever files are added or removed.
      Returns:
      the "Recent Files" menu; never null
    • getRecentFiles

      public javafx.collections.ObservableList<File> getRecentFiles()
      Returns an unmodifiable observable list of the current recent files.
      Returns:
      the recent files; never null
    • onOpenFileProperty

      public final javafx.beans.property.ObjectProperty<Consumer<File>> onOpenFileProperty()
      The callback invoked when the user clicks a recent file entry in the menu.
      Returns:
      the property holding the open-file callback
      See Also:
    • getOnOpenFile

      public final Consumer<File> getOnOpenFile()
      Gets the value of the onOpenFile property.
      Property description:
      The callback invoked when the user clicks a recent file entry in the menu.
      Returns:
      the value of the onOpenFile property
      See Also:
    • setOnOpenFile

      public final void setOnOpenFile(Consumer<File> callback)
      Sets the value of the onOpenFile property.
      Property description:
      The callback invoked when the user clicks a recent file entry in the menu.
      Parameters:
      callback - the value for the onOpenFile property
      See Also:
    • getMaxFiles

      public final int getMaxFiles()
      Returns the maximum number of recent files tracked and shown in the menu. Defaults to 5.
      Returns:
      the current maximum
    • setMaxFiles

      public final void setMaxFiles(int max)
      Sets the maximum number of recent files tracked and shown in the menu. If the current list exceeds the new maximum, oldest entries are removed.
      Parameters:
      max - the new maximum; must be >= 0