Class UI
JComponent
types to form a UI tree.
Instances of these builder type expose an API based on chained methods
designed around functional interfaces to enable building UI tree structures for Swing
in an HTML-like nested fashion while also keeping a high degree of control and transparency
by peeking into the underlying swing components or registering user actions through lambdas.
Swing tree works especially well alongside MigLayout
s,
which is why this general purpose LayoutManager
is integrated into this library.
Simply pass String
constraints to the UIForAnySwing.withLayout(String, String)
and any given UIForAnySwing.add(String, UIForAnySwing[])
method
or variant of, to make use of mig layouts.
Please take a look at the living swing-tree documentation where you can browse a large collection of examples demonstrating how to use the API of this class.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
A general purpose enum describing if something is never, always or sometimes active.static enum
Vertical or horizontal alignment.static enum
The logical combination of a vertical and horizontal alignment.static enum
Used to define how a layout manager (typically theBoxLayout
) will lay out components along the given axis.static class
{inheritDoc}static class
{inheritDoc}static class
{inheritDoc}static class
{inheritDoc}static final class
ThisColor
class is a refined and more complete/modernized implementation of theColor
class which models colors in the default sRGB color space or colors in arbitrary color spaces identified by aColorSpace
.static class
{inheritDoc}static class
{inheritDoc}static enum
Defines the areas of a component, which is used to by theImageConf
to determine if and how an image should be clipped.static enum
Enum representing the different boundaries of a UI component.static enum
Use this to specify the orientation of a component.static enum
Use this in the style API (seeUIForAnySwing.withStyle(Styler)
) to target specific corners of aJComponent
and apply customStyleConf
properties to them.static enum
An enum set of all the available swing cursors which map to the cursor type id.static enum
Used to specify the cycle method for a gradient conf in the style API.static enum
Use this to target specific edges of aJComponent
and apply customStyleConf
properties to them.static class
{inheritDoc}static enum
This enum is used to specify how an image or icon (usually aSvgIcon
) should be scaled to fit the dimensions of the component that it is being rendered into, like for example through theSvgIcon.paintIcon(java.awt.Component, Graphics, int, int)
method.static final class
This class represents a SwingTree font and is used to specify the font of a component.static enum
Use this to specify the font style of a component.static class
static enum
Use these enum instances to specify the gradient type for various sub styles, like for example the gradient style API exposed byComponentStyleDelegate.gradient(Layer, String, Configurator)
orComponentStyleDelegate.gradient(Configurator)
methods (seeUIForAnySwing.withStyle(Styler)
).static enum
Different positions along a horizontally aligned UI component.static class
{inheritDoc}static enum
Instances of this enum are used to configure onto which layer a particular style configuration should be applied.static class
{inheritDoc}static enum
Defines whether the list based data model of aJTable
is row or column major and whether it is editable or not.static enum
Set of enum instances defining common types of Swing look and feels.static enum
Defines whether the data model of aJTable
should be editable or not.static class
{inheritDoc}static class
{inheritDoc}static class
{inheritDoc}static enum
Defines the different types of noise functions that can be used to render aNoiseConf
style.static enum
static enum
Overflow policy of UI components.static class
{inheritDoc}static class
{inheritDoc}static enum
Use this to specify the placement of an image as part of theImageConf
through theImageConf.placement(Placement)
method exposed by the style API (seeUIForAnySwing.withStyle(Styler)
).static class
{inheritDoc}static class
{inheritDoc}static class
{inheritDoc}static class
{inheritDoc}static class
{inheritDoc}static enum
All UI components are at their core rectangular, meaning they always have exactly 4 uniquely identifiable sides.static class
{inheritDoc}static enum
Use these enum instances to specify the gradient alignment for various sub styles, like for example the gradient style API exposed byComponentStyleDelegate.gradient(Configurator)
orComponentStyleDelegate.gradient(Configurator)
methods (seeUIForAnySwing.withStyle(Styler)
).static class
{inheritDoc}static class
An implementation of a "split button" where the left (larger) side acts like a normal button and the right side down arrow based button opens an attachedJPopupMenu
.static class
{inheritDoc}static class
{inheritDoc}static class
{inheritDoc}static class
{inheritDoc}static class
{inheritDoc}static class
{inheritDoc}static class
{inheritDoc}static class
{inheritDoc}static class
{inheritDoc}static class
{inheritDoc}static class
{inheritDoc}static enum
Different positions along a vertically aligned UI component. -
Field Summary
Fields inherited from class swingtree.UILayoutConstants
ALIGN_CENTER, ALIGN_LEFT, ALIGN_RIGHT, ALIGN_X_CENTER, ALIGN_X_LEFT, ALIGN_X_RIGHT, ALIGN_Y_BOTTOM, ALIGN_Y_CENTER, ALIGN_Y_TOP, BOTTOM, CENTER, DEBUG, DOCK_EAST, DOCK_NORTH, DOCK_SOUTH, DOCK_WEST, FILL, FILL_X, FILL_Y, FLOW_X, FLOW_Y, GAP_BOTTOM_PUSH, GAP_LEFT_PUSH, GAP_RIGHT_PUSH, GAP_TOP_PUSH, GROW, GROW_X, GROW_Y, LEFT, NO_CACHE, NO_GRID, PUSH, PUSH_X, PUSH_Y, RIGHT, SHRINK, SHRINK_X, SHRINK_Y, SPAN, TOP, WRAP
-
Method Summary
Modifier and TypeMethodDescriptionstatic UI.LookAndFeel
SwingTree tries to be compatible with different look and feels, which is why it maintains a set of constants for the most common look and feels through theUI.LookAndFeel
enum.static void
A convenience method forstatic <T> T
A convenience method forSwingUtilities.invokeAndWait(Runnable)
, where the runnable is a lambda expression that has a return value.static void
A convenience method forSwingUtilities.invokeLater(Runnable)
, which causesRunnable
to be executed asynchronously on the AWT event dispatching thread after the specified delay has passed in the given time unit.static void
A convenience method forSwingUtilities.invokeLater(Runnable)
, which causesRunnable
to be executed asynchronously on the AWT event dispatching thread after the specified delay.static void
A convenience method forSwingUtilities.invokeLater(Runnable)
, which causes a providedRunnable
to be executed asynchronously on the AWT event dispatching thread.static void
A convenience method forSwingUtilities.invokeAndWait(Runnable)
, causesdoRun.run()
to be executed synchronously on the AWT event dispatching thread.static float
scale()
There are two types of strategies for achieving high DPI scaling in Swing.static double
scale
(double value) Multiplies the given double value by the user scale factor.static float
scale
(float value) Multiplies the given float value by the user scale factor.static int
scale
(int value) Multiplies the given int value by the user scale factor and rounds the result.static Dimension
Scales the given dimension with the user scale factor.static Ellipse2D
Takes an ellipse and scales it with the user scale factor or returns the provided ellipse if the user scale factor is 1.static RoundRectangle2D
scale
(RoundRectangle2D rectangle) Returns a rectangle from the given rectangle with the user scale factor applied.static void
scale
(Graphics2D g) If user scale factor is not 1, scale the given graphics context by invokingGraphics2D.scale(double, double)
with user scale factor.static Insets
Scales the given insets with the user scale factor.static Rectangle
Returns a rectangle from the given rectangle with the user scale factor applied.int
scaleRoundedDown
(int value) Similar asscale(int)
but always "rounds down".static void
sync()
Use this to synchronize with the UI thread from a non-UI thread.static boolean
Returns true if the current thread is an AWT event dispatching thread.static float
unscale
(float value) Divides the given float value by the user scale factor.static int
unscale
(int value) Divides the given int value by the user scale factor and rounds the result.Methods inherited from class swingtree.UIFactoryMethods
animateFor, animateFor, animateFor, animateFor, animateFor, ask, ask, ask, boldLabel, boldLabel, box, box, box, box, box, box, box, box, box, box, box, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, buttonWithIcon, checkBox, checkBox, checkBox, checkBox, checkBoxMenuItem, checkBoxMenuItem, checkBoxMenuItem, checkBoxMenuItem, checkBoxMenuItem, checkBoxMenuItem, checkBoxMenuItem, choice, choice, color, color, color, color, color, comboBox, comboBox, comboBox, comboBox, comboBox, comboBox, comboBox, comboBox, comboBox, comboBox, comboBox, comboBox, comboBox, comboBox, comboBox, comboBox, comboBox, comboBox, comboBox, comboBox, comboBox, comboBox, comboBox, comboBox, comboBox, comboBoxWithUnmodifiable, comboBoxWithUnmodifiable, comboBoxWithUnmodifiable, comboBoxWithUnmodifiable, confirm, confirm, confirmation, dialog, dialog, dialog, dialog, editorPane, error, error, findIcon, findIcon, findSvgIcon, findSvgIcon, font, formattedTextField, formattedTextField, formattedTextField, formattedTextField, frame, frame, html, html, icon, icon, icon, icon, icon, icon, info, info, label, label, label, label, label, label, label, label, label, labelWithIcon, lifeTime, list, list, list, list, list, list, list, listOf, listOf, listOf, menu, menu, menu, menuItem, menuItem, menuItem, menuItem, menuItem, menuItem, menuItem, menuItem, menuItem, message, numericTextField, numericTextField, numericTextField, numericTextField, of, of, of, of, of, of, of, of, of, of, of, of, of, of, of, of, of, panel, panel, panel, panel, panel, panel, panel, panel, panel, panel, panel, passwordField, passwordField, passwordField, passwordField, popupMenu, progressBar, progressBar, progressBar, progressBar, progressBar, progressBar, progressBar, progressBar, progressBar, progressBar, radioButton, radioButton, radioButton, radioButton, radioButton, radioButton, radioButtonMenuItem, radioButtonMenuItem, radioButtonMenuItem, radioButtonMenuItem, radioButtonMenuItem, radioButtonMenuItem, radioButtonMenuItem, radioButtonMenuItem, scrollPane, scrollPanels, scrollPanels, scrollPanels, separator, separator, separator, show, show, show, show, show, show, showUsing, showUsing, slider, slider, slider, slider, slider, slider, spinner, spinner, spinner, spinner, spinner, splitButton, splitButton, splitButton, splitItem, splitItem, splitPane, splitPane, splitRadioItem, tab, tab, tab, tab, tabbedPane, tabbedPane, tabbedPane, tabbedPane, tabbedPane, tabbedPane, table, table, table, table, table, tableHeader, textArea, textArea, textArea, textField, textField, textField, textField, textField, textField, textField, textField, textPane, toggleButton, toggleButton, toggleButton, toggleButton, toggleButton, toggleButton, toggleButton, toggleButton, toggleButton, toggleButton, toggleButton, toggleButton, toggleButton, toggleButton, toggleButtonWithIcon, toggleButtonWithIcon, toolBar, toolBar, toolBar, use, use, warn, warn
-
Method Details
-
currentLookAndFeel
SwingTree tries to be compatible with different look and feels, which is why it maintains a set of constants for the most common look and feels through theUI.LookAndFeel
enum. This method returns the current look and feel of the application orUI.LookAndFeel.OTHER
if the look and feel is not recognized.- Returns:
- One of
or
UI.LookAndFeel.OTHER
if none of the above was recognized.
-
scale
public static float scale()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.uiScale
to scale theAffineTransform
of theGraphics2D
graphics context. The second one is look and feel / client code dependent scaling, which is what this library uses.The factor returned by this method is determined by SwingTree automatically to ensure that the UI is DPI aware. All the SwingTree styles are scaled using this factor. Use this factor to scale your
Graphics2D
based painting operations. For configuring the scaling factor, seeSwingTree.setUiScaleFactor(float)
orSwingTree.initialiseUsing(SwingTreeConfigurator)
.- Returns:
- The current UI scale factor, which is used for DPI aware painting and layouts.
-
scale
public static float scale(float value) Multiplies the given float value by the user scale factor. SeeSwingTree
for more information about how the user scale factor is determined.- Parameters:
value
- The float value to scale.- Returns:
- The scaled float value.
-
scale
public static double scale(double value) Multiplies the given double value by the user scale factor. SeeSwingTree
for more information about how the user scale factor is determined.- Parameters:
value
- The double value to scale.- Returns:
- The scaled double value.
-
scale
public static int scale(int value) Multiplies the given int value by the user scale factor and rounds the result. SeeSwingTree
for more information about how the user scale factor is determined.- Parameters:
value
- The int value to scale.- Returns:
- The scaled int value.
-
scaleRoundedDown
public int scaleRoundedDown(int value) Similar asscale(int)
but always "rounds down".For use in special cases.
scale(int)
is the preferred method.- Parameters:
value
- The value to scale and then round down if the scaled result is not a whole number.- Returns:
- The scaled and rounded down value.
-
unscale
public static float unscale(float value) Divides the given float value by the user scale factor. SeeSwingTree
for more information about how the user scale factor is determined.- Parameters:
value
- The float value to unscale.- Returns:
- The unscaled float value.
-
unscale
public static int unscale(int value) Divides the given int value by the user scale factor and rounds the result. SeeSwingTree
for more information about how the user scale factor is determined.- Parameters:
value
- The int value to unscale.- Returns:
- The unscaled int value.
-
scale
If user scale factor is not 1, scale the given graphics context by invokingGraphics2D.scale(double, double)
with user scale factor. SeeSwingTree
for more information about how the user scale factor is determined.- Parameters:
g
- The graphics context to scale.
-
scale
Scales the given dimension with the user scale factor.If user scale factor is 1, then the given dimension is simply returned. Otherwise, a new instance of
Dimension
orDimensionUIResource
is returned, depending on whether the passed dimension implementsUIResource
. SeeSwingTree
for more information about how the user scale factor is determined.- Parameters:
dimension
- The dimension to scale.- Returns:
- The scaled dimension.
-
scale
Returns a rectangle from the given rectangle with the user scale factor applied.If user scale factor is 1, then the given rectangle is simply returned. Otherwise, a new instance of
Rectangle
orUIResource
is returned. SeeSwingTree
for more information about how the user scale factor is determined.- Parameters:
rectangle
- The rectangle to scale.- Returns:
- The scaled rectangle.
-
scale
Returns a rectangle from the given rectangle with the user scale factor applied.If user scale factor is 1, then the given rectangle is simply returned. Otherwise, a new instance of
Rectangle
orUIResource
is returned. SeeSwingTree
for more information about how the user scale factor is determined.- Parameters:
rectangle
- The rectangle to scale.- Returns:
- The scaled rectangle.
-
scale
Takes an ellipse and scales it with the user scale factor or returns the provided ellipse if the user scale factor is 1.- Parameters:
ellipse
- The ellipse to scale to the current UI scale factor.- Returns:
- The scaled ellipse.
-
scale
Scales the given insets with the user scale factor.If user scale factor is 1, then the given insets is simply returned. Otherwise, a new instance of
Insets
orInsetsUIResource
is returned, depending on whether the passed dimension implementsUIResource
.- Parameters:
insets
- The insets to scale.- Returns:
- The scaled insets.
-
run
A convenience method for
, which causes runnable.run() to be executed asynchronously on the AWT event dispatching thread if this current thread is not already the AWT thread. The 'invokeLater' execution will happen after all pending AWT events have been processed. This method should be used when an application thread needs to update the GUI.if ( !UI.thisIsUIThread() ) SwingUtilities.invokeLater(runnable); else runnable.run();
- Parameters:
runnable
- the instance ofRunnable
- See Also:
-
runLater
A convenience method forSwingUtilities.invokeLater(Runnable)
, which causes a providedRunnable
to be executed asynchronously on the AWT event dispatching thread. This will happen after all pending AWT events have been processed. This method should be used when an application thread needs to update the GUI. In the following example therunLater
call queues theRunnable
lambda on the event dispatching thread and then prints a message.
If runLater is called from the event dispatching thread -- for example, from a JButton's ActionListener -- theUI.run( () -> System.out.println("Hello World on " + Thread.currentThread()) ); System.out.println("This might well be displayed before the other message.");
Runnable
will still be deferred until all pending events have been processed. Note that if theRunnable
throws an uncaught exception the event dispatching thread will unwind (not the current thread).- Parameters:
runnable
- the instance ofRunnable
- See Also:
-
runLater
A convenience method forSwingUtilities.invokeLater(Runnable)
, which causesRunnable
to be executed asynchronously on the AWT event dispatching thread after the specified delay. This method should be used when an application thread needs to update the GUI after a particular delay. In the following example theinvokeLater
call queues theRunnable
lambda containing a print statement on the event dispatching thread and then prints a message.
If runLater is called from the event dispatching thread -- for example, from a JButton's ActionListener -- theUI.runLater( 1000, () -> System.out.println("Hello World on " + Thread.currentThread()) ); System.out.println("This might well be displayed before the other message.");
Runnable
will still be deferred until the specified delay has passed. Note that if theRunnable
throws an uncaught exception the event dispatching thread will unwind (not the current thread).- Parameters:
delay
- The delay in milliseconds.runnable
- the instance ofRunnable
- See Also:
-
runLater
A convenience method forSwingUtilities.invokeLater(Runnable)
, which causesRunnable
to be executed asynchronously on the AWT event dispatching thread after the specified delay has passed in the given time unit. This method should be used when an application thread needs to update the GUI after a particular delay. In the following example theinvokeLater
call queues theRunnable
lambda containing a print statement on the event dispatching thread and then prints a message.
If runLater is called from the event dispatching thread -- for example, from a JButton's ActionListener -- theUI.runLater( 1000, TimeUnit.MILLISECONDS, () -> System.out.println("Hello World on " + Thread.currentThread()) ); System.out.println("This might well be displayed before the other message.");
Runnable
will still be deferred until the specified delay has passed. Note that if theRunnable
throws an uncaught exception the event dispatching thread will unwind (not the current thread).- Parameters:
delay
- The delay in the given time unit.unit
- The time unit of the delay parameter.runnable
- the instance ofRunnable
- See Also:
-
thisIsUIThread
public static boolean thisIsUIThread()Returns true if the current thread is an AWT event dispatching thread.This method is just a cover for
javax.swing.SwingUtilities.isEventDispatchThread()
and indirectly also forjava.awt.EventQueue.isDispatchThread()
.- Returns:
- true if the current thread is an AWT event dispatching thread
-
runNow
A convenience method forSwingUtilities.invokeAndWait(Runnable)
, causesdoRun.run()
to be executed synchronously on the AWT event dispatching thread. This call blocks until all pending AWT events have been processed and (then)doRun.run()
returns. This method should be used when an application thread needs to update the GUI. It shouldn't be called from the event dispatching thread. Here's an example that creates a new application thread that usesinvokeAndWait
to print a string from the event dispatching thread and then, when that's finished, print a string from the application thread.
Note that contrary to thevar appThread = new Thread(() -> { try { UI.runNow(() -> { System.out.println("Hello World on " + Thread.currentThread()); }); } catch (Exception e) { e.printStackTrace(); } System.out.println("Finished on " + Thread.currentThread()); }); appThread.start();
SwingUtilities.invokeAndWait(Runnable)
method, this method does not throw an exception if it is called from the event dispatching thread. Instead, it just executes the runnable immediately.- Parameters:
runnable
- the instance ofRunnable
- See Also:
-
runAndGet
A convenience method forSwingUtilities.invokeAndWait(Runnable)
, where the runnable is a lambda expression that has a return value. This causes theSupplier
to be executed synchronously on the AWT event dispatching thread. This call blocks until all pending AWT events have been processed and (then) theSupplier
returns. This method should be used when an application thread needs to update the GUI a get a return value from the GUI. It shouldn't be called from the event dispatching thread. Here's an example that creates a new application thread that usesrunAndGet(..)
to access the state of aJCheckBox
from the event dispatching thread and then, when that's finished, print the state from the application thread.JCheckBox checkBox = new JCheckBox("Hello World"); var appThread = new Thread(()->{ try { boolean state = UI.runAndGet(() -> checkBox.isSelected()); System.out.println("CheckBox state is " + state); } catch (Exception e) { e.printStackTrace(); } System.out.println("Finished on " + Thread.currentThread()); }); appThread.start();
- Type Parameters:
T
- The return type of the result value produced by the supplier.- Parameters:
supplier
- The supplier which should be executed on the UI thread.- Returns:
- The result provided by the supplier.
-
sync
public static void sync()Use this to synchronize with the UI thread from a non-UI thread. After calling this method, the current thread will be blocked until the UI thread has finished executing all of its pending events. This method should only be called from the application thread and not from the UI thread.
-