Class RecentFiles
java.lang.Object
com.dlsc.gemsfx.util.RecentFiles
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
PropertiesTypePropertyDescriptionThe callback invoked when the user clicks a recent file entry in the menu. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault maximum number of recent file entries shown in the menu.static final StringDefault preferences key under which file paths are stored. -
Constructor Summary
ConstructorsConstructorDescriptionRecentFiles(Preferences preferences) Creates aRecentFilesmanager using the givenPreferencesnode.RecentFiles(Preferences preferences, String prefsKey) Creates aRecentFilesmanager using the givenPreferencesnode and a custom preferences key. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds the given file to the top of the recent files list and persists it immediately.voidclear()Clears the recent files list and persists the change immediately.final intReturns the maximum number of recent files tracked and shown in the menu.javafx.scene.control.MenugetMenu()Returns theMenuthat reflects the current recent files list.Gets the value of theonOpenFileproperty.javafx.collections.ObservableList<File> Returns an unmodifiable observable list of the current recent files.The callback invoked when the user clicks a recent file entry in the menu.final voidsetMaxFiles(int max) Sets the maximum number of recent files tracked and shown in the menu.final voidsetOnOpenFile(Consumer<File> callback) Sets the value of theonOpenFileproperty.
-
Property Details
-
onOpenFile
-
-
Field Details
-
DEFAULT_MAX_FILES
public static final int DEFAULT_MAX_FILESDefault maximum number of recent file entries shown in the menu.- See Also:
-
DEFAULT_PREFS_KEY
Default preferences key under which file paths are stored.- See Also:
-
-
Constructor Details
-
RecentFiles
Creates aRecentFilesmanager using the givenPreferencesnode. File paths are stored under the key "recent.files".- Parameters:
preferences- the preferences node used for persistence; must not benull
-
RecentFiles
Creates aRecentFilesmanager using the givenPreferencesnode and a custom preferences key.- Parameters:
preferences- the preferences node used for persistence; must not benullprefsKey- the key under which file paths are stored; must not benull
-
-
Method Details
-
add
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 ifnull
-
clear
public void clear()Clears the recent files list and persists the change immediately. -
getMenu
public javafx.scene.control.Menu getMenu()Returns theMenuthat reflects the current recent files list. Add this to aMenuBar. The menu updates automatically whenever files are added or removed.- Returns:
- the "Recent Files" menu; never
null
-
getRecentFiles
Returns an unmodifiable observable list of the current recent files.- Returns:
- the recent files; never
null
-
onOpenFileProperty
-
getOnOpenFile
-
setOnOpenFile
-
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
-