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 MigLayouts,
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
Nested ClassesModifier and TypeClassDescriptionstatic enumA general purpose enum describing if something is never, always or sometimes active.static enumVertical or horizontal alignment.static enumThe logical combination of a vertical and horizontal alignment.static enumUsed 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 classThisColorclass is a refined and more complete/modernized implementation of theColorclass 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 enumDefines the areas of a component, which is used to by theImageConfto determine if and how an image should be clipped.static enumEnum representing the different boundaries of a UI component.static enumUse this to specify the orientation of a component.static enumUse this in the style API (seeUIForAnySwing.withStyle(Styler)) to target specific corners of aJComponentand apply customStyleConfproperties to them.static enumAn enum set of all the available swing cursors which map to the cursor type id.static enumUsed to specify the cycle method for a gradient conf in the style API.static enumThis enum contains constant values representing the type of action(s) to be performed by a Drag and Drop operation.static enumUse this to target specific edges of aJComponentand apply customStyleConfproperties to them.static class{inheritDoc}static enumThis 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 classThis class represents a SwingTree font and is used to specify the font of a component.static enumUse this to specify the font style of a component.static classstatic enumUse these enum instances to specify the gradient type for various sub styles, like for example the gradient style API exposed byComponentStyleDelegate.gradient(swingtree.UI.Layer, String, Configurator)orComponentStyleDelegate.gradient(Configurator)methods (seeUIForAnySwing.withStyle(Styler)).static enumDifferent positions along a horizontally aligned UI component.static class{inheritDoc}static enumInstances of this enum are used to configure onto which layer a particular style configuration should be applied.static enumDefines whether the list based data model of aJTableis row or column major and whether it is editable or not.static class{inheritDoc}static enumSet of enum instances defining common types of Swing look and feels.static enumDefines whether the data model of aJTableshould be editable or not.static class{inheritDoc}static class{inheritDoc}static class{inheritDoc}static enumDefines the different types of noise functions that can be used to render aNoiseConfstyle.static enumstatic enumOverflow policy of UI components.static class{inheritDoc}static class{inheritDoc}static enumUse this to specify the placement of an image as part of theImageConfthrough theImageConf.placement(swingtree.UI.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 class{inheritDoc}static enumAll UI components are at their core rectangular, meaning they always have exactly 4 uniquely identifiable sides.static class{inheritDoc}static enumUse 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 classstatic 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 enumDifferent 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.LookAndFeelSwingTree 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.LookAndFeelenum.static voidA convenience method forstatic <T> TA convenience method forSwingUtilities.invokeAndWait(Runnable), where the runnable is a lambda expression that has a return value.static voidA convenience method forSwingUtilities.invokeLater(Runnable), which causesRunnableto be executed asynchronously on the AWT event dispatching thread after the specified delay has passed in the given time unit.static voidA convenience method forSwingUtilities.invokeLater(Runnable), which causesRunnableto be executed asynchronously on the AWT event dispatching thread after the specified delay.static voidA convenience method forSwingUtilities.invokeLater(Runnable), which causes a providedRunnableto be executed asynchronously on the AWT event dispatching thread.static voidA convenience method forSwingUtilities.invokeAndWait(Runnable), causesdoRun.run()to be executed synchronously on the AWT event dispatching thread.static floatscale()There are two types of strategies for achieving high DPI scaling in Swing.static doublescale(double value) Multiplies the given double value by the user scale factor.static floatscale(float value) Multiplies the given float value by the user scale factor.static intscale(int value) Multiplies the given int value by the user scale factor and rounds the result.static DimensionScales the given dimension with the user scale factor.static Ellipse2DTakes an ellipse and scales it with the user scale factor or returns the provided ellipse if the user scale factor is 1.static RoundRectangle2Dscale(RoundRectangle2D rectangle) Returns a rectangle from the given rectangle with the user scale factor applied.static voidscale(Graphics2D g) If user scale factor is not 1, scale the given graphics context by invokingGraphics2D.scale(double, double)with user scale factor.static InsetsScales the given insets with the user scale factor.static RectangleReturns a rectangle from the given rectangle with the user scale factor applied.intscaleRoundedDown(int value) Similar asscale(int)but always "rounds down".static voidsync()Use this to synchronize with the UI thread from a non-UI thread.static booleanReturns true if the current thread is an AWT event dispatching thread.static floatunscale(float value) Divides the given float value by the user scale factor.static intunscale(int value) Divides the given int value by the user scale factor and rounds the result.Methods inherited from class swingtree.UIFactoryMethods
animate, animate, 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, 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, scaleIconTo, scrollPane, scrollPane, scrollPanels, scrollPanels, scrollPanels, separator, separator, separator, show, show, show, show, show, show, showUsing, showUsing, slider, slider, slider, slider, slider, slider, slider, slider, spinner, spinner, spinner, spinner, spinner, splitButton, splitButton, 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, warnMethods inherited from class swingtree.UILayoutConstants
AC, ALIGN, AUTO_SPAN, AUTO_SPAN, CC, DOCK, GAP_BOTTOM, GAP_LEFT, GAP_REL, GAP_RIGHT, GAP_TOP, GROW, GROW_X, GROW_Y, HEIGHT, INS, INS, INSETS, INSETS, LC, PAD, PAD, PUSH, PUSH_X, PUSH_Y, SHRINK, SHRINK_PRIO, SHRINK_X, SHRINK_Y, SKIP, SPAN, SPAN, SPAN_X, SPAN_Y, SPLIT, WIDTH, WRAP
-
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.LookAndFeelenum. This method returns the current look and feel of the application orUI.LookAndFeel.OTHERif the look and feel is not recognized.- Returns:
- One of
or
UI.LookAndFeel.OTHERif 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.uiScaleto scale theAffineTransformof theGraphics2Dgraphics 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
Graphics2Dbased 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. SeeSwingTreefor 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. SeeSwingTreefor 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. SeeSwingTreefor 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. SeeSwingTreefor 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. SeeSwingTreefor 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. SeeSwingTreefor 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
DimensionorDimensionUIResourceis returned, depending on whether the passed dimension implementsUIResource. SeeSwingTreefor 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
RectangleorUIResourceis returned. SeeSwingTreefor 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
RectangleorUIResourceis returned. SeeSwingTreefor 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
InsetsorInsetsUIResourceis 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 providedRunnableto 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 therunLatercall queues theRunnablelambda 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.");Runnablewill still be deferred until all pending events have been processed. Note that if theRunnablethrows 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 causesRunnableto 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 theinvokeLatercall queues theRunnablelambda 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.");Runnablewill still be deferred until the specified delay has passed. Note that if theRunnablethrows 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 causesRunnableto 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 theinvokeLatercall queues theRunnablelambda 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.");Runnablewill still be deferred until the specified delay has passed. Note that if theRunnablethrows 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 usesinvokeAndWaitto 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 theSupplierto be executed synchronously on the AWT event dispatching thread. This call blocks until all pending AWT events have been processed and (then) theSupplierreturns. 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 aJCheckBoxfrom 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.
-