public final class UIForCombo<E,C extends JComboBox<E>>
extends UIForAnySwing<UIForCombo<E,C>,JComboBox<E>>
A SwingTree builder node designed for configuring
JComboBox
instances.
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.
-
Method Summary
Modifier and TypeMethodDescriptionprotected final <B extends swingtree.UIForAnything<?,
?, JComponent>>
void_addBuildersTo
(JComboBox<E> thisComponent, B... builders) protected final void
_addBuilderTo
(JComboBox<E> thisComponent, swingtree.UIForAnything<?, ?, ?> builder, @Nullable Object conf) protected final void
_addComponentsTo
(JComboBox<E> thisComponent, JComponent... componentsToBeAdded) protected final void
This method is used to dispose of the state of the builder, which means that the builder state disposes of its reference to either the wrapped component or the wrapped component or the composite of component factories which are used to build the wrapped component eagerly each time the wrapped component is accessed.protected UIForCombo<E,
C> _newBuilderWithState
(swingtree.BuilderState<JComboBox<E>> newState) An internal wither method which creates a new builder instance with the providedBuilderState
stored inside it.protected final <T> void
Use this to register a state change listener for the provided property which will be executed by the UI thread (seeEventProcessor
).protected final <T> void
_onShow
(sprouts.Vals<T> vals, JComboBox<E> c, BiConsumer<JComboBox<E>, sprouts.ValsDelegate<T>> displayAction) Use this to register a state change listener for the provided property list which will be executed by the UI thread (seeEventProcessor
).protected final void
A convenient delegate to theEventProcessor.registerAppEvent(Runnable)
method, which allows you to execute an action on the current application thread.protected final <T> void
A convenient delegate to theEventProcessor.registerAppEvent(Runnable)
method, which allows you to execute an action on the current application thread.protected final void
A convenient shortcut to theEventProcessor.registerUIEvent(Runnable)
method to the currentEventProcessor
attached to the currentBuilderState
._state()
Returns the state of the builder, which is a container for the wrapped component as well as it's type and currentEventProcessor
.protected final UIForCombo<E,
C> _this()
Exposes the this-pointer of the builder instance cast to theI
type parameter of the builder class.protected final swingtree.UIForAnything<UIForCombo<E,
C>, JComboBox<E>, JComponent> Creates a new builder with the provided component mutation applied to the wrapped component.protected final <T> swingtree.UIForAnything<UIForCombo<E,
C>, JComboBox<E>, JComponent> _withOnShow
(sprouts.Val<T> val, BiConsumer<JComboBox<E>, T> displayAction) protected final <T> swingtree.UIForAnything<UIForCombo<E,
C>, JComboBox<E>, JComponent> _withOnShow
(sprouts.Vals<T> vals, BiConsumer<JComboBox<E>, sprouts.ValsDelegate<T>> displayAction) final <V extends E>
UIForCombo<E,C> _withRenderer
(RenderBuilder<C, V> renderBuilder) final <B extends swingtree.UIForAnything<?,
?, JComponent>>
UIForCombo<E,C> add
(B... builders) This method provides the same functionality as the other "add" methods.final UIForCombo<E,
C> add
(JComponent... components) This builder class expects its implementations to be builder types for anything which can be built in a nested tree-like structure.final UIForCombo<E,
C> add
(List<JComponent> components) This builder class expects its implementations to be builder types for anything which can be built in a nested tree-like structure.final <T extends JComponent>
UIForCombo<E,C> add
(UIForAnySwing<?, T> builder) Uses the supplied builder to build its component and then add it to the component that is being built by this builder instance.final UIForCombo<E,
C> apply
(Consumer<UIForCombo<E, C>> building) Use this to continue building UI inside a provided lambda if you need to introduce some imperative code in between the building process.final UIForCombo<E,
C> applyIf
(boolean condition, Consumer<UIForCombo<E, C>> building) Use this to only build a certain part of the UI if the provided boolean condition is true.final UIForCombo<E,
C> applyIfPresent
(Optional<Consumer<UIForCombo<E, C>>> building) Allows you to build declarative UI conditionally, meaning that the UI is only built if the providedOptional
value is present.final OptionalUI<JComboBox<E>>
Deprecated.final boolean
This method completes the building process for the wrappedJComponent
type by returning it.Builds and returns the configuredJComboBox
instance.Deprecated.Useget(Class)
instead.getType()
The type class of the component managed by this builder.final int
hashCode()
UIForCombo<E,
C> isEditableIf
(boolean isEditable) Use this to enable or disable editing for the wrapped UI component.UIForCombo<E,
C> isEditableIf
(sprouts.Var<Boolean> isEditable) Use this to enable or disable editing of the wrapped UI component through property binding dynamically.UIForCombo<E,
C> onCancel
(sprouts.Action<ComponentDelegate<C, PopupMenuEvent>> action) Registers a listener to be notified when the combo box is canceled, meaning its popup menu is hidden which typically happens when the user clicks outside the combo box.UIForCombo<E,
C> onClose
(sprouts.Action<ComponentDelegate<C, PopupMenuEvent>> action) Registers a listener to be notified when the combo box is closed, meaning its popup menu is hidden after the user clicks on the combo box.UIForCombo<E,
C> onEnter
(sprouts.Action<ComponentDelegate<C, ActionEvent>> action) Adds anActionListener
to the editor component of the underlyingJComboBox
which will be called when a selection has been made.UIForCombo<E,
C> onOpen
(sprouts.Action<ComponentDelegate<C, PopupMenuEvent>> action) Registers a listener to be notified when the combo box is opened, meaning its popup menu is shown after the user clicks on the combo box.UIForCombo<E,
C> onSelection
(sprouts.Action<ComponentDelegate<JComboBox<E>, ActionEvent>> action) Adds anAction
to the underlyingJComboBox
through anActionListener
, which will be called when a selection has been made.final UIForCombo<E,
C> Use this if you wish to access the component wrapped by this builder directly.final String
toString()
final UIForCombo<E,
C> withCellRenderer
(ListCellRenderer<E> renderer) Sets theListCellRenderer
for theJComboBox
, which renders the combo box items by supplying a custom component for each item through theListCellRenderer.getListCellRendererComponent(JList, Object, int, boolean, boolean)
method.final UIForCombo<E,
C> Uses the given list of elements as a basis for a new combo box model and sets it as the model for the combo box.final UIForCombo<E,
C> Uses a read only property list of elements as a basis for a new combo box model and sets it as the model for the combo box.final UIForCombo<E,
C> Uses the given selection property as well as an array of elements as a basis for a new combo box model and sets it as the new model for the combo box state.final UIForCombo<E,
C> Uses the provided selection property as well as a list of elements as a basis for a new combo box model.final UIForCombo<E,
C> Uses the given selection property as well as a read only property of an array of elements as a basis for a new combo box model and sets it as the new model for the combo box state.final UIForCombo<E,
C> Uses the given selection property as well as a read only property list of elements as a basis for a new combo box model and sets it as the new model for the combo box state.final UIForCombo<E,
C> Uses the given selection property as well as a property of an array of elements as a basis for a new combo box model and sets it as the new model for the combo box state.final UIForCombo<E,
C> Uses the given selection property as well as a property list of elements as a basis for a new combo box model and sets it as the new model for the combo box state.final UIForCombo<E,
C> Uses the given property list of elements as a basis for a new combo box model and sets it as the model for the combo box.final UIForCombo<E,
C> withModel
(ComboBoxModel<E> model) Use this convenience method to specify the model for the combo box, which is used by the combo box component to determine the available options and the currently selected item.final <V extends E>
UIForCombo<E,C> withRenderer
(Configurator<RenderBuilder<C, V>> renderBuilder) Use this to define a generic combo box renderer for various item types..final UIForCombo<E,
C> withSelectedItem
(E item) This method allows you to specify an initial selection for the combo box.final UIForCombo<E,
C> withSelectedItem
(sprouts.Var<E> item) Use this to dynamically set the selected item of the combo box.final UIForCombo<E,
C> withTextRenderer
(Function<CellDelegate<C, E>, String> renderer) Use this to specify a custom text based cell renderer for each item in the combo box.Methods inherited from class swingtree.UIForAnySwing
_addComponentTo, _addViewableProps, _isUndefinedColor, _isUndefinedFont, _setBackground, _setEnabled, _setMinHeight, _setMinWidth, _setPrefWidth, add, add, add, add, add, add, add, add, add, add, add, add, add, doUpdates, group, group, id, id, isEnabledIf, isEnabledIf, isEnabledIf, isEnabledIfNot, isEnabledIfNot, isEnabledIfNot, isFocusableIf, isFocusableIf, isFocusableIf, isFocusableIfNot, isFocusableIfNot, isFocusableIfNot, isValidIf, isVisibleIf, isVisibleIf, isVisibleIf, isVisibleIfNot, isVisibleIfNot, isVisibleIfNot, makeFocused, makeNonOpaque, makeOpaque, on, on, onCharTyped, onFocusGain, onFocusLoss, onHidden, onKeyPress, onKeyRelease, onKeyTyped, onMouseClick, onMouseDrag, onMouseEnter, onMouseExit, onMouseMove, onMousePress, onMouseRelease, onMouseWheelDown, onMouseWheelMove, onMouseWheelUp, onMoved, onPressed, onRelease, onResize, onShown, onTyped, onTyped, onView, withBackground, withBackground, withBackgroundIf, withBackgroundIf, withBackgroundIf, withBackgroundIf, withBorder, withBorder, withBorderTitled, withBorderTitled, withBoxLayout, withCompoundBorder, withCompoundBorderTitled, withCursor, withCursor, withCursorIf, withCursorIf, withEmptyBorder, withEmptyBorder, withEmptyBorder, withEmptyBorder, withEmptyBorderTitled, withEmptyBorderTitled, withEmptyBorderTitled, withEmptyBorderTitled, withEmptyBorderTitled, withEmptyBorderTitled, withEmptyBorderTitled, withEmptyBorderTitled, withFlowLayout, withFlowLayout, withFlowLayout, withForeground, withForeground, withForegroundIf, withForegroundIf, withForegroundIf, withForegroundIf, withGridBagLayout, withGridLayout, withGridLayout, withGridLayout, withHeight, withHeight, withLayout, withLayout, withLayout, withLayout, withLayout, withLayout, withLayout, withLayout, withLayout, withLayout, withLayout, withLayout, withLineBorder, withLineBorder, withLineBorder, withLineBorderTitled, withLineBorderTitled, withLineBorderTitled, withLineBorderTitled, withMatteBorder, withMatteBorder, withMatteBorder, withMatteBorderTitled, withMatteBorderTitled, withMatteBorderTitled, withMaxHeight, withMaxHeight, withMaxSize, withMaxSize, withMaxSize, withMaxSize, withMaxWidth, withMaxWidth, withMinHeight, withMinHeight, withMinSize, withMinSize, withMinSize, withMinSize, withMinWidth, withMinWidth, withPrefHeight, withPrefHeight, withPrefSize, withPrefSize, withPrefSize, withPrefSize, withPrefWidth, withPrefWidth, withProperty, withRepaintOn, withRoundedLineBorder, withRoundedLineBorder, withRoundedLineBorder, withRoundedLineBorder, withRoundedLineBorderTitled, withRoundedLineBorderTitled, withRoundedLineBorderTitled, withRoundedLineBorderTitled, withRoundedLineBorderTitled, withRoundedLineBorderTitled, withRoundedLineBorderTitled, withSize, withSize, withSize, withStyle, withTooltip, withTooltip, withTransitionalStyle, withTransitoryStyle, withWidth, withWidth
-
Method Details
-
getComboBox
Builds and returns the configuredJComboBox
instance.- Returns:
- The configured
JComboBox
instance.
-
_state
Returns the state of the builder, which is a container for the wrapped component as well as it's type and currentEventProcessor
.- Returns:
- The state of the builder.
-
_newBuilderWithState
An internal wither method which creates a new builder instance with the providedBuilderState
stored inside it.- Parameters:
newState
- The new state which should be stored inside the new builder instance.- Returns:
- A new builder instance with the provided state stored inside it.
-
onOpen
Registers a listener to be notified when the combo box is opened, meaning its popup menu is shown after the user clicks on the combo box.- Parameters:
action
- the action to be executed when the combo box is opened.- Returns:
- this
-
onClose
Registers a listener to be notified when the combo box is closed, meaning its popup menu is hidden after the user clicks on the combo box.- Parameters:
action
- the action to be executed when the combo box is closed.- Returns:
- this
-
onCancel
Registers a listener to be notified when the combo box is canceled, meaning its popup menu is hidden which typically happens when the user clicks outside the combo box.- Parameters:
action
- the action to be executed when the combo box is canceled.- Returns:
- this
-
onSelection
public UIForCombo<E,C> onSelection(sprouts.Action<ComponentDelegate<JComboBox<E>, ActionEvent>> action) Adds anAction
to the underlyingJComboBox
through anActionListener
, which will be called when a selection has been made. If the combo box is editable, then anActionEvent
will be fired when editing has stopped. For more information seeJComboBox.addActionListener(ActionListener)
.- Parameters:
action
- TheAction
that will be notified.- Returns:
- This very instance, which enables builder-style method chaining.
- Throws:
IllegalArgumentException
- ifaction
isnull
.
-
onEnter
Adds anActionListener
to the editor component of the underlyingJComboBox
which will be called when a selection has been made. If the combo box is editable, then anActionEvent
will be fired when editing has stopped. For more information seeJComboBox.addActionListener(ActionListener)
.- Parameters:
action
- TheAction
that will be notified.- Returns:
- This very builder instance, which allows for method chaining.
-
isEditableIf
Use this to enable or disable editing for the wrapped UI component.- Parameters:
isEditable
- The truth value determining if the UI component should be editable or not.- Returns:
- This very instance, which enables builder-style method chaining.
-
isEditableIf
Use this to enable or disable editing of the wrapped UI component through property binding dynamically.- Parameters:
isEditable
- The boolean property determining if the UI component should be editable or not.- Returns:
- This very instance, which enables builder-style method chaining.
- Throws:
IllegalArgumentException
- ifisEditable
isnull
.
-
_withRenderer
-
withRenderer
public final <V extends E> UIForCombo<E,C> withRenderer(Configurator<RenderBuilder<C, V>> renderBuilder) Use this to define a generic combo box renderer for various item types.. You would typically want to use this method to render generic types where the only common type isObject
, yet you still want to render the items in a specific way depending on their actual type.
This is done like so:
Note that inside the lambda function, you can use theUI.comboBox(new Object[]{":-)", 42L, '§'}) .withRenderer( it -> it .when(String.class).asText( cell -> "String: "+cell.getValue() ) .when(Character.class).asText( cell -> "Char: "+cell.getValue() ) .when(Number.class).asText( cell -> "Number: "+cell.getValue() ) );
RenderBuilder
to define for what type of item you want to render the item in a specific way and theRenderAs
to define how the item should be rendered.You may want to know that a similar API is also available for the
JList
andJTable
components, seeUIForList.withRenderer(Configurator)
,UIForTable.withRenderer(Configurator)
andUIFactoryMethods.table(Configurator)
for more information.- Type Parameters:
V
- The type of the value that is being rendered in this combo box.- Parameters:
renderBuilder
- A lambda function that configures the renderer for this combo box.- Returns:
- This combo box instance for further configuration.
-
withCellRenderer
Sets theListCellRenderer
for theJComboBox
, which renders the combo box items by supplying a custom component for each item through theListCellRenderer.getListCellRendererComponent(JList, Object, int, boolean, boolean)
method.- Parameters:
renderer
- TheListCellRenderer
that will be used to paint each cell in the combo box.- Returns:
- This very instance, which enables builder-style method chaining.
-
withTextRenderer
Use this to specify a custom text based cell renderer for each item in the combo box. The renderer is a function that takes aCellDelegate
as input and returns aString
which will be used as the text for the combo box item.- Parameters:
renderer
- The function that will be used to render the combo box items.- Returns:
- This very instance, which enables builder-style method chaining.
-
withModel
Use this convenience method to specify the model for the combo box, which is used by the combo box component to determine the available options and the currently selected item.- Parameters:
model
- TheComboBoxModel
to be used for modelling the content data of the combo box.- Returns:
- This very instance, which enables builder-style method chaining.
-
withItems
Uses the given list of elements as a basis for a new combo box model and sets it as the model for the combo box. This means that whenever the list of elements changes, and the combo box is rendered, the combo box will be updated accordingly.- Parameters:
options
- The list of elements to be used as the basis for the combo box model.- Returns:
- This builder node, which enables builder-style method chaining.
- Throws:
NullPointerException
- ifoptions
isnull
.
-
withItems
Uses the provided selection property as well as a list of elements as a basis for a new combo box model. Whenever the selection or the list of elements changes, and the combo box is rendered, the combo box will be updated accordingly. Note that the use of theVar
type for the selection property allows the combo box to listen for changes to the selection property, which ensures that the combo box is updated whenever the selection property changes.- Parameters:
selection
- The selection property to be used as the basis for modelling the currently selected item in a new combo box model.options
- The list of elements to be used as the basis for modelling the available options in a new combo box model.- Returns:
- This builder node, which enables builder-style method chaining.
- Throws:
NullPointerException
- ifselection
oroptions
isnull
.
-
withItems
Uses the given property list of elements as a basis for a new combo box model and sets it as the model for the combo box. The combo box will register a change listener and update itself whenever the list of elements changes.- Parameters:
options
- The property list of elements to be used as the basis for a new combo box model.- Returns:
- This builder node, which enables builder-style method chaining.
- Throws:
NullPointerException
- ifoptions
isnull
.
-
withItems
Uses a read only property list of elements as a basis for a new combo box model and sets it as the model for the combo box. The combo box will register a change listener and update itself whenever the list of elements changes. Due to the fact that the list of elements is read only, changes to the list of elements can only come from the view model.- Parameters:
options
- The read only property list of elements to be used as the basis for a new combo box model.- Returns:
- This builder node, which enables builder-style method chaining.
- Throws:
NullPointerException
- ifoptions
isnull
.
-
withItems
Uses the given selection property as well as a property list of elements as a basis for a new combo box model and sets it as the new model for the combo box state. This means that whenever the state of the selection property or the property list of elements changes, then combo box will be updated and rendered accordingly.- Parameters:
selection
- The selection property to be used as the basis for modelling the currently selected item in a new combo box model.options
- The property list of elements to be used as the basis for modelling the available options in a new combo box model.- Returns:
- This builder node instance, which allows for builder-style method chaining.
- Throws:
NullPointerException
- if either one ofselection
oroptions
isnull
.
-
withItems
Uses the given selection property as well as a read only property list of elements as a basis for a new combo box model and sets it as the new model for the combo box state. This means that whenever the state of the selection property or the read only property list of elements changes, then combo box will be updated and rendered according to said changes. Due to the list of options being read only, changes to it can only come from the view model.- Parameters:
selection
- The selection property to be used as the basis for modelling the currently selected item in a new combo box model.options
- The read only property list of elements to be used as the basis for modelling the available options in a new combo box model.- Returns:
- This builder node, which allows for builder-style method chaining.
- Throws:
NullPointerException
- if either one ofselection
oroptions
isnull
.
-
withItems
Uses the given selection property as well as an array of elements as a basis for a new combo box model and sets it as the new model for the combo box state. This means that whenever the state of the selection property or the array of elements changes, then combo box will be updated and rendered according to said changes. Note that the combo box can not register change listeners on the array of elements, which means that for the combo box to be updated whenever the array of elements changes, you must trigger the update manually.- Parameters:
selection
- The selection property to be used as the basis for modelling the currently selected item in a new combo box model.options
- The array of elements to be used as the basis for modelling the available options in a new combo box model.- Returns:
- This builder node, which allows for builder-style method chaining.
- Throws:
NullPointerException
- if either one ofselection
oroptions
isnull
.
-
withItems
Uses the given selection property as well as a property of an array of elements as a basis for a new combo box model and sets it as the new model for the combo box state. This means that whenever the state of the selection property or the property of an array of elements changes, then combo box will be updated and rendered according to said changes.- Parameters:
selection
- The selection property to be used as the basis for modelling the currently selected item in a new combo box model.options
- The property of an array of elements to be used as the basis for modelling the available options in a new combo box model.- Returns:
- This builder node, which allows for builder-style method chaining.
- Throws:
NullPointerException
- if either one ofselection
oroptions
isnull
.
-
withItems
Uses the given selection property as well as a read only property of an array of elements as a basis for a new combo box model and sets it as the new model for the combo box state. This means that whenever the state of the selection property or the read only property of an array of elements changes, then combo box will be updated and rendered according to said changes. Due to the list of options being read only, changes to it can only come from the view model.- Parameters:
selection
- The selection property to be used as the basis for modelling the currently selected item in a new combo box model.options
- The read only property of an array of elements to be used as the basis for modelling the available options in a new combo box model.- Returns:
- This builder node, which allows for builder-style method chaining.
- Throws:
NullPointerException
- if either one ofselection
oroptions
isnull
.
-
withSelectedItem
This method allows you to specify an initial selection for the combo box.- Parameters:
item
- The item which should be set as the currently selected combo box item.- Returns:
- This very instance, which enables builder-style method chaining.
-
withSelectedItem
Use this to dynamically set the selected item of the combo box.- Parameters:
item
- The item to be selected.- Returns:
- This very instance, which enables builder-style method chaining.
-
getType
The type class of the component managed by this builder. See documentation for method "build" for more information.- Returns:
- The type class of the component managed by this builder.
-
getComponent
Deprecated.Useget(Class)
instead.The component managed by this builder.- Returns:
- The component managed by this builder.
- Throws:
IllegalStateException
- if this method is called from a thread other than the EDT and this UI is configured to be decoupled from the application thread. SeeUIFactoryMethods.use(EventProcessor, Supplier)
.
-
component
Deprecated.Useget(Class)
instead.The optional component managed by this builder.- Returns:
- An
OptionalUI
wrapping a component or null. This optional will throw an exception if the application has an application thread (seeUIFactoryMethods.use(EventProcessor, Supplier)
) and this method is called from a thread other than the EDT.
-
peek
Use this if you wish to access the component wrapped by this builder directly. This is useful for more fine-grained control, like for example calling methods like "setName", "setTitle", and so on...
This method accepts a lambda to which the component wrapped by this builder will be supplied. The lambda can then call said methods or perform other tasks which might be relevant to the component while also not breaking the benefits of nesting and method chaining provided by this class...
The below example shows how this method allows for more fine-grained control over the wrapped component:UI.panel() peek( panel -> panel.setDebugGraphicsOptions(true) );
- Parameters:
action
- A Consumer lambda which simply returned the wrapped JComponent type for interacting it.- Returns:
- This very instance, which enables builder-style method chaining.
-
applyIf
Use this to only build a certain part of the UI if the provided boolean condition is true. Which is to say, if the condition is false, then the second lambda is ignored, if on the other hand the condition is true, then the second lambda is executed with the current builder instance passed to it as a parameter. Inside the lambda, one can then continue building the UI while also not breaking the benefits of nesting and method chaining provided by this builder...This is in essence a more advanced version of
apply(Consumer)
.
Here a simple usage example:
Here we use theis method to build a panel with different content depending on whether the user is logged in or not.UI.panel() .applyIf( userIsLoggedIn, ui -> ui .add( UI.label("Welcome back!") ) .add( UI.button("Logout")).onClick( () -> logout() ) .add( UI.button("Settings")).onClick( () -> showSettings() ) ) .applyIf( !userIsLoggedIn, ui -> ui .add( UI.label("Please login to continue.") ) .add( UI.button("Login")).onClick( () -> login() ); );
- Parameters:
condition
- The truth value which determines if the second consumer lambda is executed or not.building
- AConsumer
lambda which simply consumes this builder instance.- Returns:
- This very instance, which enables builder-style method chaining.
-
applyIfPresent
Allows you to build declarative UI conditionally, meaning that the UI is only built if the providedOptional
value is present. If the value is not present, meaning it is null, then the second lambda (containing UI declarations relevant to the value) is simply ignored.Consider the following example:
The// In your view model: public Optional<MySubModel> getM() { return Optional.ofNullable(this.model); } // In your view: UI.panel() .add(UI.label("Maybe Sub Model:")) .applyIfPresent(vm.getM().map(m->ui->ui .add(UI.label("Hello Sub Model!")) .add(UI.label("A:") .add(UI.textField(m.getA())) .add(UI.label("B:")) .add(UI.textField(m.getB())) // ... )) .add(UI.label("Some other stuff..."));
applyIfPresent
method takes anOptional<Consumer<I>>
as parameter, whereI
is the type of the UI builder. This allows you to map the optional value to a consumer which is only executed if the value is present. If the optional value is present, the consumer is executed with the current UI builder as a parameter, which allows you to continue building the UI as usual.
Them->ui->ui
may look a bit confusing at first, but it is simply a lambda expression which takes the optional value and returns a consumer (ui->ui...
) which takes the UI builder as a parameter.
This is in essence a more advancedOptional
centric version ofapplyIf(boolean, Consumer)
andapply(Consumer)
.- Parameters:
building
- An optional consumer lambda which simply consumes this builder node.- Returns:
- This very instance, which enables builder-style method chaining.
-
apply
Use this to continue building UI inside a provided lambda if you need to introduce some imperative code in between the building process.
This is especially useful for when you need to build UI based on loops. The current builder instance will simply be supplied to the providedConsumer
lambda. Inside the supplied lambda, you can then continue building the UI while also not breaking the benefits of nesting and method chaining, effectively preserving the declarative nature of the builder.
Here is a simple example of how this method can be used to build a panel with a variable amount of images displayed in a grid:UI.panel("wrap 3") .apply( ui -> { for ( String path : imagePaths ) ui.add( UI.label(UI.icon(path)) ); });
Here is another example of how this method can be used to build a panel with a variable amount of buttons displayed in a grid:UI.panel("wrap 4") .apply( ui -> { for ( int i = 0; i < numOfButtons; i++ ) ui.add( UI.button("Button " + i) .onClick( () -> {...} ); });
- Parameters:
building
- A Consumer lambda which simply consumes this builder instance.- Returns:
- This very instance, which enables builder-style method chaining.
-
get
This method completes the building process for the wrappedJComponent
type by returning it. However, it also expects the user to pass the class of theJComponent
wrapped by this builder! This is not out of necessity but for better readability when using the builder in more extensive ways where the beginning and end of the method chaining and nesting of the builder does not fit on one screen.
In such cases the expression ".get(MyJComponent.class)
" helps to identify which type ofJComponent
is currently being built on a given nesting layer...
Here is a simple example that demonstrates this technique using aJPanel
and aJMenuBar
:
As you can see, the expression "UI.panel() .add( UI.menuBar() .add( UI.menu("File") ) .add( UI.menuItem("Open") ) .add( UI.menuItem("Save") ) // ... .add( UI.menuItem("Exit") ) .get(JMenuBar.class) ) .add( UI.button("Click me!") ) .get(JPanel.class);
.get(JMenuBar.class)
" as well as the expression ".get(JPanel.class)
" at the end of the builder chain help to identify which type ofJComponent
is currently being built and returned.- Parameters:
type
- The type class of the component which this builder wraps.- Returns:
- The result of the building process, namely: a type of JComponent.
-
add
This builder class expects its implementations to be builder types for anything which can be built in a nested tree-like structure. Implementations of this abstract method ought to enable support for nested building.- Parameters:
components
- An array of component instances which ought to be added to the wrapped component type.- Returns:
- This very instance, which enables builder-style method chaining.
-
add
Uses the supplied builder to build its component and then add it to the component that is being built by this builder instance. This directly allows you to nest your builder based UI declarations in an HTML-like fashion.- Type Parameters:
T
- The type of theJComponent
which is wrapped by the provided builder.- Parameters:
builder
- A builder for anotherJComponent
instance which ought to be added to the wrapped component type.- Returns:
- This very instance, which enables builder-style method chaining.
-
add
@SafeVarargs public final <B extends swingtree.UIForAnything<?,?, UIForCombo<E,JComponent>> C> add(B... builders) This method provides the same functionality as the other "add" methods. However, it bypasses the necessity to call the "get" method by calling it internally for you.
This helps to improve readability, especially when the degree of nesting is very low.- Type Parameters:
B
- The type of the builder instances which are used to configure the components that will be added to the component wrapped by this builder.- Parameters:
builders
- An array of builder instances whose JComponents ought to be added to the one wrapped by this builder.- Returns:
- This very instance, which enables builder-style method chaining.
-
add
This builder class expects its implementations to be builder types for anything which can be built in a nested tree-like structure. Implementations of this abstract method ought to enable support for nested building.- Parameters:
components
- A list of component instances which ought to be added to the wrapped component type.- Returns:
- This very instance, which enables builder-style method chaining.
-
_addBuildersTo
@SafeVarargs protected final <B extends swingtree.UIForAnything<?,?, void _addBuildersToJComponent>> (JComboBox<E> thisComponent, B... builders) -
_addComponentsTo
@SafeVarargs protected final void _addComponentsTo(JComboBox<E> thisComponent, JComponent... componentsToBeAdded) -
_addBuilderTo
-
_with
protected final swingtree.UIForAnything<UIForCombo<E,C>, _withJComboBox<E>, JComponent> (Consumer<JComboBox<E>> componentMutator) Creates a new builder with the provided component mutation applied to the wrapped component.
Note that the SwingTree builders are immutable, which means that this method does not mutate the current builder instance, but instead creates a new builder instance with a newBuilderState
which contains the provided component mutation (seeBuilderState.withMutator(Consumer)
). Also see_newBuilderWithState(BuilderState)
.- Parameters:
componentMutator
- A consumer lambda which receives the wrapped component and is then used to apply some builder action to it.- Returns:
- A new builder instance with the provided component mutation applied to the wrapped component.
-
_runInUI
A convenient shortcut to theEventProcessor.registerUIEvent(Runnable)
method to the currentEventProcessor
attached to the currentBuilderState
. In practice, this method will ultimately just delegate tasks to the AWT Event Dispatch Thread (EDT).- Parameters:
action
- An action which should be executed by the UI thread, which is determined by implementations of theEventProcessor
, also seeUIFactoryMethods.use(EventProcessor, Supplier)
.
Usually the UI thread is AWT's Event Dispatch Thread (EDT).
-
_runInApp
A convenient delegate to theEventProcessor.registerAppEvent(Runnable)
method, which allows you to execute an action on the current application thread. To configure the currentEventProcessor
seeUIFactoryMethods.use(EventProcessor, Supplier)
or the underlyingSwingTree.setEventProcessor(EventProcessor)
method.- Parameters:
action
- An action which should be executed by the application thread, which is determined by implementations of the currentEventProcessor
, also seeUIFactoryMethods.use(EventProcessor, Supplier)
.
-
_runInApp
A convenient delegate to theEventProcessor.registerAppEvent(Runnable)
method, which allows you to execute an action on the current application thread. Which thread executes these tasks is determined by the currentEventProcessor
. Usually this is theEventProcessor.COUPLED
orEventProcessor.COUPLED_STRICT
event processor.- Type Parameters:
T
- The type of the value.- Parameters:
value
- A value which should be captured and then passed to the provided action on the current application thread (seeEventProcessor
andUIFactoryMethods.use(EventProcessor, Supplier)
).action
- A consumer lambda which is executed by the application thread and receives the provided value.
-
_onShow
protected final <T> void _onShow(sprouts.Val<T> val, JComboBox<E> thisComponent, BiConsumer<JComboBox<E>, T> displayAction) Use this to register a state change listener for the provided property which will be executed by the UI thread (seeEventProcessor
).- Type Parameters:
T
- The type of the item wrapped by the provided property.- Parameters:
val
- A property whose state changes should be listened to on the UI thread.thisComponent
- The component which is wrapped by this builder.displayAction
- A consumer lambda receiving the provided value and is then executed by the UI thread.
-
_withOnShow
protected final <T> swingtree.UIForAnything<UIForCombo<E,C>, _withOnShowJComboBox<E>, JComponent> (sprouts.Val<T> val, BiConsumer<JComboBox<E>, T> displayAction) -
_onShow
protected final <T> void _onShow(sprouts.Vals<T> vals, JComboBox<E> c, BiConsumer<JComboBox<E>, sprouts.ValsDelegate<T>> displayAction) Use this to register a state change listener for the provided property list which will be executed by the UI thread (seeEventProcessor
).- Type Parameters:
T
- The type of the items wrapped by the provided property list.- Parameters:
vals
- A property list whose state changes should be listened to on the UI thread.c
- The component which is wrapped by this builder.displayAction
- A consumer lambda receiving the action delegate and is then executed by the UI thread.
-
_withOnShow
protected final <T> swingtree.UIForAnything<UIForCombo<E,C>, _withOnShowJComboBox<E>, JComponent> (sprouts.Vals<T> vals, BiConsumer<JComboBox<E>, sprouts.ValsDelegate<T>> displayAction) -
_this
Exposes the this-pointer of the builder instance cast to theI
type parameter of the builder class.
This is done to reduce the amount of type casting and warnings in the codebase.- Returns:
- The builder instance itself based on the type parameter
<I>
.
-
_disposeState
protected final void _disposeState()This method is used to dispose of the state of the builder, which means that the builder state disposes of its reference to either the wrapped component or the wrapped component or the composite of component factories which are used to build the wrapped component eagerly each time the wrapped component is accessed.
This is important to avoid memory leaks, as a component is typically part of a tree of components, and if one component is not garbage collected, then the whole tree is not garbage collected. -
hashCode
public final int hashCode() -
equals
-
toString
-
get(Class)
instead.