Class ThemeUtilities
java.lang.Object
org.tentackle.fx.ThemeUtilities
- Direct Known Subclasses:
RdcThemeUtilities
Utility methods for theme management.
Maintains the dark mode and the theme name, applies the stylesheets to the scenes, and saves/loads the user's theme preferences. The default implementation supports the JavaFX default theme Modena only. Theme modules such as
Maintains the dark mode and the theme name, applies the stylesheets to the scenes, and saves/loads the user's theme preferences. The default implementation supports the JavaFX default theme Modena only. Theme modules such as
tentackle-fx-atlanta replace the singleton to provide more themes.- Author:
- harald
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordTheme preferences. -
Property Summary
PropertiesTypePropertyDescriptionjavafx.beans.property.BooleanPropertyGets the dark mode property.
Changing the property updates the stylesheets of all scenes.javafx.beans.property.StringPropertyGets the theme name property.
Changing the property updates the stylesheets of all scenes. -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates the theme utilities with the default theme name.ThemeUtilities(String defaultThemeName, int fontSize) Creates the theme utilities. -
Method Summary
Modifier and TypeMethodDescriptionvoidapplyStylesheets(javafx.scene.Scene scene) Applies the default stylesheets to a scene.
Invoked for all newly created scenes or viareapplyStyleSheets().voidapplyThemePreferences(ThemeUtilities.ThemePreferences preferences) Applies the theme preferences.
Preferences that are null leave the current setting unchanged.voidApplies the user agent stylesheet.
The default implementation does nothing, i.e. keeps the JavaFX default Modena.javafx.beans.property.BooleanPropertyGets the dark mode property.
Changing the property updates the stylesheets of all scenes.doubleGets the padding for auto-adjusting the width of a table column or tree table column.javafx.scene.text.FontGets the default font.static ThemeUtilitiesThe singleton.getResourceURL(Class<?> clazz, String name) Gets the resource URL.
The method is provided to allow redirects to other resources, usually CSS files.Gets the list of supported themes.Gets the current theme name.booleanReturns whether dark mode is enabled.loadPreferences(Class<?> clazz) Loads the theme name and dark mode from the user preferences on the local machine.loadPreferences(Class<?> clazz, Boolean defaultDarkMode, String defaultThemeName) Loads the theme name and dark mode from the user preferences on the local machine.voidResets and applies the stylesheets to all scenes.
Useful when the dark mode configuration changed, for example.voidsavePreferences(Class<?> clazz, ThemeUtilities.ThemePreferences preferences) Saves the theme name and dark mode to the user preferences on the local machine.voidsetDarkMode(boolean darkMode) Enables dark mode.voidSets the theme name and dark mode simultaneously.
The stylesheets are updated only once.voidsetThemeName(String themeName) Sets the theme name.javafx.beans.property.StringPropertyGets the theme name property.
Changing the property updates the stylesheets of all scenes.protected voidUpdates the stylesheets according to the dark mode and theme setting.
Does nothing if not invoked from the FX application thread or whilesetTheme(boolean, String)is in progress.
-
Property Details
-
darkMode
public javafx.beans.property.BooleanProperty darkModePropertyGets the dark mode property.
Changing the property updates the stylesheets of all scenes.- See Also:
-
themeName
public javafx.beans.property.StringProperty themeNamePropertyGets the theme name property.
Changing the property updates the stylesheets of all scenes.- See Also:
-
-
Field Details
-
DARK_MODE
-
THEME_NAME
-
-
Constructor Details
-
ThemeUtilities
Creates the theme utilities.- Parameters:
defaultThemeName- the default theme namefontSize- the default font size
-
ThemeUtilities
public ThemeUtilities()Creates the theme utilities with the default theme name.
-
-
Method Details
-
getInstance
-
isDarkMode
public boolean isDarkMode()Returns whether dark mode is enabled.- Returns:
- true if dark mode, false if light mode (default)
-
setDarkMode
public void setDarkMode(boolean darkMode) Enables dark mode.- Parameters:
darkMode- true to enable dark theme, false for light theme (default)
-
darkModeProperty
public javafx.beans.property.BooleanProperty darkModeProperty()Gets the dark mode property.
Changing the property updates the stylesheets of all scenes.- Returns:
- the property
- See Also:
-
getSupportedThemes
-
getThemeName
-
setThemeName
Sets the theme name.Throws
FxRuntimeExceptionif the theme is not supported.- Parameters:
themeName- the theme name
-
themeNameProperty
public javafx.beans.property.StringProperty themeNameProperty()Gets the theme name property.
Changing the property updates the stylesheets of all scenes.- Returns:
- the property
- See Also:
-
setTheme
Sets the theme name and dark mode simultaneously.
The stylesheets are updated only once.- Parameters:
darkMode- the dark mode flagthemeName- the theme name
-
savePreferences
Saves the theme name and dark mode to the user preferences on the local machine.- Parameters:
clazz- the application classpreferences- the preferences to save
-
loadPreferences
public ThemeUtilities.ThemePreferences loadPreferences(Class<?> clazz, Boolean defaultDarkMode, String defaultThemeName) Loads the theme name and dark mode from the user preferences on the local machine.- Parameters:
clazz- the application classdefaultDarkMode- the default dark mode value, null for nonedefaultThemeName- the default theme name, null for none- Returns:
- the preferences, never null
-
loadPreferences
Loads the theme name and dark mode from the user preferences on the local machine.- Parameters:
clazz- the application class- Returns:
- the preferences, never null
-
applyThemePreferences
Applies the theme preferences.
Preferences that are null leave the current setting unchanged.- Parameters:
preferences- the preferences
-
applyUserAgentStylesheet
public void applyUserAgentStylesheet()Applies the user agent stylesheet.
The default implementation does nothing, i.e. keeps the JavaFX default Modena. Overridden by theme implementations. -
applyStylesheets
public void applyStylesheets(javafx.scene.Scene scene) Applies the default stylesheets to a scene.
Invoked for all newly created scenes or viareapplyStyleSheets().- Parameters:
scene- the scene
-
reapplyStyleSheets
public void reapplyStyleSheets()Resets and applies the stylesheets to all scenes.
Useful when the dark mode configuration changed, for example. -
getResourceURL
-
getDefaultFont
public javafx.scene.text.Font getDefaultFont()Gets the default font.- Returns:
- the default font
-
getAutoAdjustPadding
public double getAutoAdjustPadding()Gets the padding for auto-adjusting the width of a table column or tree table column.- Returns:
- the padding in pixels
-
updateStyles
protected void updateStyles()Updates the stylesheets according to the dark mode and theme setting.
Does nothing if not invoked from the FX application thread or whilesetTheme(boolean, String)is in progress.
-