Class SwingTree
SwingTree is a singleton that holds global configuration context for the SwingTree library.
This includes the EventProcessor that is used to process events, as well as the
StyleSheet that is used to style components.
You may access the singleton instance of the
SwingTree class through the get() method.-
Method Summary
Modifier and TypeMethodDescriptionapplyScaleAsFontSize(Font font) Converts the current scale factor given in system property "swingtree.uiScale" to a font size and then returns a new font derived from the provided one, with that new size!static voidclear()Clears the singleton instance of theSwingTree.static SwingTreeget()Returns the singleton instance of theSwingTree.sprouts.Viewable<AWTEvent> getAwtEventView(long mask) This method constitutes a memory safe alternative toToolkit.addAWTEventListener(AWTEventListener, long)as it returns a weakly referenced reactive property that will automatically be garbage collected alongside its change listeners when you no longer reference it strongly in your code.
You use the reactiveViewablereturned by this method to register change listeners that will be invoked whenever an AWT event matching the given mask is fired anywhere in the application.
The suppliedeventMaskis a bitmask of event types to receive.longReturns the default animation interval in milliseconds which is a property that determines the delay between two consecutive animation steps.Returns the keystroke shortcut that is used to toggle the SwingTree dev-tool, which is a classical inspector tool that creates an overlay on top of the UI and shows debug information about the component under the mouse cursor.TheEventProcessoris a simple interface whose implementations delegate tasks to threads that are capable of processing GUI or application events.The icon cache is a hash map that uses anIconDeclarationas a key and anImageIconas a value.TheStyleSheetis an abstract class whose extensions are used to declare component styles through a CSS like DSL API.Exposes a set of system properties in the form of a nicely formatted string.doubleReturns the system scale factor.doubleReturns the system scale factor for the given graphics context.floatThere are two types of strategies for achieving high DPI scaling in Swing.sprouts.Viewable<Float> Creates and returns a reactiveViewableof the library context's user scale factor which will update itself and invoke all of its change listeners when the user scale factor changes, through methods likesetUiScaleFactor(float).
If you no longer reference a reactive property view strongly in your code, then it will be garbage collected alongside all of its change listeners automatically for you!static voidA lazy initialization of the singleton instance of theSwingTreeclass causing it to be recreated the next time it is requested throughget().
This is useful for testing purposes, also in cases where the UI scale changes (through the reference font).
Also seeinitializeUsing(SwingTreeConfigurator).static voidinitializeUsing(SwingTreeConfigurator configurator) A lazy initialization of the singleton instance of theSwingTreeclass causing it to be recreated the next time it is requested throughget(),
but with aSwingTreeConfiguratorthat is used to configure theSwingTreeinstance.
This is useful for testing purposes, but also in cases where the UI scale must be initialized or changed manually (through the reference font).
Also seeinitialize().booleanReturns whether the SwingTree dev-tool is currently enabled or not.sprouts.Viewable<Boolean> Creates and returns a reactiveViewableof the library context's dev-tool enabled state which will update itself and invoke all of its change listeners when the dev-tool enabled state changes, through methods likesetDevToolEnabled(boolean)or through the keystroke shortcut returned bygetDevToolKeyStrokeShortcut().
If you no longer reference a reactive property view strongly in your code, then it will be garbage collected alongside all of its change listeners automatically for you!
The dev-tool is a classical inspector tool that creates an overlay on top of the UI and shows debug information about the component under the mouse cursor.booleanReturns whether the library is configured to record debug source traces for components during their creation.
If this is the case, then SwingTree will record the stack trace of a component at the moment of its creation.booleanReturns whether system scaling is enabled.booleanReturns whether the UI scaling mode is enabled as is specified by the system propertyswingtree.uiScale.enabled.org.slf4j.MarkerExposes the library wide loggingMarkerused by SwingTree for all its logging.Applies a custom scale factor given in system property "swingtree.uiScale" to the given font.voidsetDefaultAnimationInterval(long defaultAnimationInterval) Sets the default animation interval in milliseconds which is a property that determines the delay between two consecutive animation steps.voidsetDevToolEnabled(boolean enabled) Enables or disables the SwingTree dev-tool, which is a classical inspector tool that creates an overlay on top of the UI and summons an additional dialog showing debug information about the component under the mouse cursor.
You can also toggle it through the keystroke shortcut returned bygetDevToolKeyStrokeShortcut(), which is "ctrl shift I" by default.voidsetEventProcessor(EventProcessor eventProcessor) Sets theEventProcessorthat is used to process GUI and application events.voidsetStyleSheet(StyleSheet styleSheet) Sets theStyleSheetthat is used to style components.voidsetUiScaleFactor(float scaleFactor) Sets the user scale factor is a scaling factor that is used by SwingTree's style engine to scale the UI during painting.
-
Method Details
-
get
Returns the singleton instance of theSwingTree. Note that this method will create the singleton if it does not exist.- Returns:
- the singleton instance of the
SwingTree.
-
clear
public static void clear()Clears the singleton instance of theSwingTree. This is useful for testing purposes, or if you want to reconfigure your application with a differentSwingTreeInitConfig. (seeinitializeUsing(SwingTreeConfigurator)). -
initialize
public static void initialize()A lazy initialization of the singleton instance of theSwingTreeclass causing it to be recreated the next time it is requested throughget().
This is useful for testing purposes, also in cases where the UI scale changes (through the reference font).
Also seeinitializeUsing(SwingTreeConfigurator). -
initializeUsing
A lazy initialization of the singleton instance of theSwingTreeclass causing it to be recreated the next time it is requested throughget(),
but with aSwingTreeConfiguratorthat is used to configure theSwingTreeinstance.
This is useful for testing purposes, but also in cases where the UI scale must be initialized or changed manually (through the reference font).
Also seeinitialize().- Parameters:
configurator- theSwingTreeConfiguratorthat is used to configure theSwingTreeinstance.
-
getAwtEventView
This method constitutes a memory safe alternative toToolkit.addAWTEventListener(AWTEventListener, long)as it returns a weakly referenced reactive property that will automatically be garbage collected alongside its change listeners when you no longer reference it strongly in your code.
You use the reactiveViewablereturned by this method to register change listeners that will be invoked whenever an AWT event matching the given mask is fired anywhere in the application.
The suppliedeventMaskis a bitmask of event types to receive. It is constructed by bitwise OR-ing together the event masks defined inAWTEvent.
There are many event ids, likeMouseEvent.BUTTON1,MouseEvent.MOUSE_CLICKED,KeyEvent.KEY_PRESSED,ComponentEvent.COMPONENT_MOVED,ComponentEvent.COMPONENT_MOVED, etc...
Note that when the
SwingTreelibrary context gets cleared throughclear()or replaced throughinitialize()orinitializeUsing(SwingTreeConfigurator), then the reactive view returned by this method will no longer receive updates and will be effectively dead!- Parameters:
mask- the bitmask of event types to receive, constructed by bitwise OR-ing together the event masks defined inAWTEvent. For example, if you want to listen to mouse clicks and key presses, you would pass inAWTEvent.MOUSE_EVENT_MASK | AWTEvent.KEY_EVENT_MASKas the mask.- Returns:
- a reactive
Viewablethat will update itself and invoke all of its change listeners whenever an AWT event matching the given mask is fired anywhere in the application. - See Also:
-
isRecordingDebugSourceTrace
public boolean isRecordingDebugSourceTrace()Returns whether the library is configured to record debug source traces for components during their creation.
If this is the case, then SwingTree will record the stack trace of a component at the moment of its creation. It will be captured as aTupleofStackTraceElements and then stored as a client properties under the "built-at" key.
This source location trace is primarily used by the SwingTree dev-tool, which can be enabled through pressing thegetDevToolKeyStrokeShortcut()keystroke shortcut. It creates an overlay on top of the UI that shows debug information about the component under the mouse cursor. When pressing control together with a left click on a component, it opens a dialog showing a lot of important debug information about the component, including the source code location trace.- Returns:
- true if the library is configured to record debug source traces for components during their creation, false otherwise.
- See Also:
-
getDevToolKeyStrokeShortcut
Returns the keystroke shortcut that is used to toggle the SwingTree dev-tool, which is a classical inspector tool that creates an overlay on top of the UI and shows debug information about the component under the mouse cursor. By default, the shortcut to enable it is "ctrl shift I", but you can configure it through theSwingTreeInitConfig.devToolKeyStrokeShortcut(String)method of theSwingTreeInitConfigclass, which is used to configure theSwingTreeinstance through theSwingTreeConfiguratorininitializeUsing(SwingTreeConfigurator).- Returns:
- The keystroke shortcut that is used to toggle the SwingTree dev-tool / inspection-tool.
- See Also:
-
isDevToolEnabled
public boolean isDevToolEnabled()Returns whether the SwingTree dev-tool is currently enabled or not. The dev-tool is a classical inspector tool that creates an overlay on top of the UI and shows debug information about the component under the mouse cursor. You can toggle it through the keystroke shortcut returned bygetDevToolKeyStrokeShortcut().- Returns:
- true if the SwingTree dev-tool is currently enabled, false otherwise.
- See Also:
-
setDevToolEnabled
public void setDevToolEnabled(boolean enabled) Enables or disables the SwingTree dev-tool, which is a classical inspector tool that creates an overlay on top of the UI and summons an additional dialog showing debug information about the component under the mouse cursor.
You can also toggle it through the keystroke shortcut returned bygetDevToolKeyStrokeShortcut(), which is "ctrl shift I" by default.
This method is useful when you want to enable or disable the dev-tool programmatically, for example through a button in your UI, or through a command in your application's console.- Parameters:
enabled- true to enable the SwingTree dev-tool, false to disable it.- See Also:
-
isDevToolEnabledView
Creates and returns a reactiveViewableof the library context's dev-tool enabled state which will update itself and invoke all of its change listeners when the dev-tool enabled state changes, through methods likesetDevToolEnabled(boolean)or through the keystroke shortcut returned bygetDevToolKeyStrokeShortcut().
If you no longer reference a reactive property view strongly in your code, then it will be garbage collected alongside all of its change listeners automatically for you!
The dev-tool is a classical inspector tool that creates an overlay on top of the UI and shows debug information about the component under the mouse cursor. You can toggle it through the keystroke shortcut returned bygetDevToolKeyStrokeShortcut(), which is "ctrl shift I" by default.- Returns:
- A reactive property holding whether the SwingTree dev-tool is currently enabled or not. You may hold onto such a view and register change listeners on it to ensure your components always know whether the dev-tool is enabled or not!
- See Also:
-
getIconCache
The icon cache is a hash map that uses anIconDeclarationas a key and anImageIconas a value. This is used to cache icons that are loaded from the file system using convenience methods likeUIFactoryMethods.findIcon(String)andUIFactoryMethods.findIcon(IconDeclaration)orUIFactoryMethods.findSvgIcon(String),UIFactoryMethods.findSvgIcon(IconDeclaration).
Note that the map returned by this method is mutable and can be used to add or remove icons from the cache. You may also want to consider this as a possible source of memory leaks.- Returns:
- The icon cache of this context, which is used to cache icons that are loaded from the file system.
-
getUiScaleFactor
public float getUiScaleFactor()There are two types of strategies for achieving high DPI scaling in Swing. The first one is available since Java 9, and it uses the system propertysun.java2d.uiScaleto scale theAffineTransformof theGraphics2Dgraphics context. The second one is look and feel / client code dependent scaling, which is what SwingTree uses in, for example, the style engine or when seting the size of component through the SwingTree API...The float based scaling factor returned by this method can be used as a multiplier in order to scale something from "developer pixels" to "component pixels" / "look and feel pixels". Conversely, by dividing a number using this factor, you convert something from "component pixels" to platform-agnostic "developer pixel".
The scaling factor is computed by SwingTree automatically based on the system font. Anything you do through the SwingTree API, will be scaled for you,
but if you write code against raw Swing, you may need to use this scale factor to ensure consistent scaling support across screens with varying DPI. Most commonly, you will need to do manual scaling when defining component dimensions or to scale customGraphics2Dbased painting operations.
You can configure this scaling factor through the library initialization methodinitializeUsing(SwingTreeConfigurator), or directly through the system property "swingtree.uiScale".
You may also set it usingsetUiScaleFactor(float).- Returns:
- The user scale factor to convert between "developer pixel size" and "component pixel size".
-
setUiScaleFactor
public void setUiScaleFactor(float scaleFactor) Sets the user scale factor is a scaling factor that is used by SwingTree's style engine to scale the UI during painting. Note that this is different from the system/Graphics2D scale factor, which is the scale factor that the JRE uses to scale everything through theAffineTransformof theGraphics2D.You can configure this scaling factor through the library initialization method
initializeUsing(SwingTreeConfigurator), or directly through the system property "swingtree.uiScale".- Parameters:
scaleFactor- The user scale factor.
-
getUiScaleView
Creates and returns a reactiveViewableof the library context's user scale factor which will update itself and invoke all of its change listeners when the user scale factor changes, through methods likesetUiScaleFactor(float).
If you no longer reference a reactive property view strongly in your code, then it will be garbage collected alongside all of its change listeners automatically for you!
The user scale factor is a scaling factor that converts "developer pixel sizes" to "component pixel sizes" and it is used by SwingTree's API to support dynamic DPI scaling.
Note that this is different from the system/Graphics2D scale factor, which is the scale factor that the JRE uses to scale everything through the
AffineTransformof theGraphics2D.You can configure this scaling factor through the library initialization method
initializeUsing(SwingTreeConfigurator), or directly through the system property "swingtree.uiScale".
Also note that when the
SwingTreelibrary context gets cleared throughclear()or replaced throughinitialize()orinitializeUsing(SwingTreeConfigurator), then the reactive view returned by this method will no longer receive updates and will be effectively dead!- Returns:
- A reactive property holding the current user scale factor used for scaling the UI of your application. You may hold onto such a view and register change listeners on it to ensure your components always have the correct scale!
-
isUiScaleFactorEnabled
public boolean isUiScaleFactorEnabled()Returns whether the UI scaling mode is enabled as is specified by the system propertyswingtree.uiScale.enabled. -
scale
Applies a custom scale factor given in system property "swingtree.uiScale" to the given font. -
applyScaleAsFontSize
Converts the current scale factor given in system property "swingtree.uiScale" to a font size and then returns a new font derived from the provided one, with that new size! -
isSystemScalingEnabled
public boolean isSystemScalingEnabled()Returns whether system scaling is enabled. System scaling means that the JRE scales everything through theAffineTransformof theGraphics2D. If this is the case, then we do not have to do scaled painting and can use the original size of icons, gaps, etc.- Returns:
- true if system scaling is enabled.
-
getSystemScaleFactorOf
Returns the system scale factor for the given graphics context. The system scale factor is the scale factor that the JRE uses to scale everything (text, icons, gaps, etc).- Parameters:
g- The graphics context to get the system scale factor for.- Returns:
- The system scale factor for the given graphics context.
-
getSystemScaleFactor
public double getSystemScaleFactor()Returns the system scale factor. The system scale factor is the scale factor that the JRE uses to scale everything (text, icons, gaps, etc) irrespective of the current look and feel, as this is the scale factor that is used by theAffineTransformof theGraphics2D.- Returns:
- The system scale factor.
-
getEventProcessor
TheEventProcessoris a simple interface whose implementations delegate tasks to threads that are capable of processing GUI or application events. As part of this singleton, the SwingTree library maintains a globalEventProcessorthat is used consistently by all declarative builders.- Returns:
- The currently configured
EventProcessorthat is used to process GUI and application events.
-
setEventProcessor
Sets theEventProcessorthat is used to process GUI and application events. You may not pass null as an argument, because SwingTree requires an event processor to function.- Parameters:
eventProcessor- theEventProcessorthat is used to process GUI and application events.- Throws:
NullPointerException- if eventProcessor is null!
-
getStyleSheet
TheStyleSheetis an abstract class whose extensions are used to declare component styles through a CSS like DSL API. As part of this singleton, the SwingTree library maintains a globalStyleSheetthat is used consistently by all declarative builders. Use this method to access this global style sheet.- Returns:
- The currently configured
StyleSheetthat is used to style components.
-
setStyleSheet
Sets theStyleSheetthat is used to style components. UseStyleSheet.none()instead of null to switch off global styling.- Parameters:
styleSheet- TheStyleSheetthat is used to style components.- Throws:
NullPointerException- if styleSheet is null!
-
getDefaultAnimationInterval
public long getDefaultAnimationInterval()Returns the default animation interval in milliseconds which is a property that determines the delay between two consecutive animation steps. You can think of it as the time between the heartbeats of the animation. The smaller the interval, the higher the refresh rate and the smoother the animation will look. However, the smaller the interval, the more CPU time will be used. The default interval is 16 ms which corresponds to almost 60 fps.
This property is used as default value by theLifeTimeobject which is used to define the duration of anAnimation. The value returned by this is used by animations if no other interval is specified throughLifeTime.withInterval(long, TimeUnit).- Returns:
- The default animation interval in milliseconds.
-
setDefaultAnimationInterval
public void setDefaultAnimationInterval(long defaultAnimationInterval) Sets the default animation interval in milliseconds which is a property that determines the delay between two consecutive animation steps. You can think of it as the time between the heartbeats of the animation. The smaller the interval, the higher the refresh rate and the smoother the animation will look. However, the smaller the interval, the more CPU time will be used. The default interval is 16 ms which corresponds to almost 60 fps.
This property is used as default value by theLifeTimeobject which is used to define the duration of anAnimation. The value returned by this is used by animations if no other interval is specified throughLifeTime.withInterval(long, TimeUnit).- Parameters:
defaultAnimationInterval- The default animation interval in milliseconds.
-
getSystemInfo
Exposes a set of system properties in the form of a nicely formatted string. These are used by the SwingTree library to determine the system configuration and to adjust the UI accordingly.- Returns:
- A string containing system information.
-
logMarker
public org.slf4j.Marker logMarker()Exposes the library wide loggingMarkerused by SwingTree for all its logging. You may use this marker to channel SwingTree logs to a separate log file or to filter them in any other way you like.
To configure the loggingMarker, check out theinitializeUsing(SwingTreeConfigurator)method, where you may provide a customSwingTreeConfiguratorthat sets the loggingMarkerthrough theSwingTreeInitConfig.- Returns:
- The logging
Markerwhich is passed to methods of the SLF4J logger.
-