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 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._state()
Returns the state of the builder, which is a container for the wrapped component as well as it's type and currentEventProcessor
.final <V extends E>
UIForCombo<E, C> _withRendererAndEditor
(CellBuilder<C, V> cellBuilder) Builds and returns the configuredJComboBox
instance.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 <V extends E>
UIForCombo<E, C> withCell
(Configurator<CellConf<C, V>> cellConfigurator) Use this method to configure how the combo box views should be rendered for a specific type of item.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 <V extends E>
UIForCombo<E, C> withCells
(Configurator<CellBuilder<C, V>> renderBuilder) Use this to define a generic combo box view/renderer for any type of item.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 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> 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, _addViewableProps, _isUndefinedColor, _isUndefinedFont, _setBackground, _setEnabled, _setMinHeight, _setMinWidth, _setPrefWidth, add, add, add, add, add, add, add, add, add, add, addAll, addAll, addAll, addAll, addAll, addAll, 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, onMouseEnter, onMouseEnterGreedy, onMouseExit, onMouseExit, onMouseExitGreedy, onMouseMove, onMousePress, onMouseRelease, onMouseWheelDown, onMouseWheelMove, onMouseWheelUp, onMoved, onPressed, onRelease, onResize, onShown, onTyped, onTyped, onView, withBackground, withBackground, withBackgroundColor, withBackgroundIf, withBackgroundIf, withBackgroundIf, withBackgroundIf, withBorder, withBorder, withBorderTitled, withBorderTitled, withBoxLayout, withCompoundBorder, withCompoundBorderTitled, withCursor, withCursor, withCursorIf, withCursorIf, withDragAway, withDropSite, withEmptyBorder, withEmptyBorder, withEmptyBorder, withEmptyBorder, withEmptyBorderTitled, withEmptyBorderTitled, withEmptyBorderTitled, withEmptyBorderTitled, withEmptyBorderTitled, withEmptyBorderTitled, withEmptyBorderTitled, withEmptyBorderTitled, withFlowLayout, withFlowLayout, withFlowLayout, withForeground, withForeground, withForegroundColor, withForegroundIf, withForegroundIf, withForegroundIf, withForegroundIf, withGridBagLayout, withGridLayout, withGridLayout, withGridLayout, withHeight, withHeight, withHeightExactly, withHeightExactly, 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, withMaxSize, withMaxWidth, withMaxWidth, withMinHeight, withMinHeight, withMinSize, withMinSize, withMinSize, withMinSize, withMinSize, withMinWidth, withMinWidth, withPrefHeight, withPrefHeight, withPrefSize, withPrefSize, withPrefSize, withPrefSize, withPrefSize, withPrefWidth, withPrefWidth, withProperty, withRepaintOn, withRepaintOn, withRepaintOn, withRepaintOn, withRepaintOn, withRepaintOn, withRoundedLineBorder, withRoundedLineBorder, withRoundedLineBorder, withRoundedLineBorder, withRoundedLineBorderTitled, withRoundedLineBorderTitled, withRoundedLineBorderTitled, withRoundedLineBorderTitled, withRoundedLineBorderTitled, withRoundedLineBorderTitled, withRoundedLineBorderTitled, withSize, withSize, withSize, withSize, withSizeExactly, withSizeExactly, withSizeExactly, withSizeExactly, withStyle, withTooltip, withTooltip, withTransitionalStyle, withTransitoryStyle, withWidth, withWidth, withWidthExactly, withWidthExactly
Methods inherited from class swingtree.UIForAnything
_addBuildersTo, _addBuilderTo, _addComponentsTo, _disposeState, _onShow, _onShow, _runInApp, _runInApp, _runInUI, _this, _with, _withOnShow, _withOnShow, add, add, add, add, apply, applyIf, applyIfPresent, component, equals, get, getComponent, getType, hashCode, peek, toString
-
Method Details
-
getComboBox
Builds and returns the configuredJComboBox
instance.- Returns:
- The configured
JComboBox
instance.
-
_state
Description copied from class:UIForAnything
Returns the state of the builder, which is a container for the wrapped component as well as it's type and currentEventProcessor
.- Specified by:
_state
in classUIForAnything<UIForCombo<E,
C extends JComboBox<E>>, JComboBox<E>, JComponent> - Returns:
- The state of the builder.
-
_newBuilderWithState
Description copied from class:UIForAnything
An internal wither method which creates a new builder instance with the providedBuilderState
stored inside it.- Specified by:
_newBuilderWithState
in classUIForAnything<UIForCombo<E,
C extends JComboBox<E>>, JComboBox<E>, JComponent> - 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
.
-
_withRendererAndEditor
-
withCells
Use this to define a generic combo box view/renderer for any type of item. 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, 'ยง'}) .withCells( 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() ) );
CellBuilder
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.withCells(Configurator)
,UIForTable.withCells(Configurator)
andUIFactoryMethods.table(Configurator)
for more information.Note that the preferred way of building a list cell view is through the
withCell(Configurator)
method, which is way simpler as it does not assume the combobox has an item type ambiguity.- 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.
-
withCell
Use this method to configure how the combo box views should be rendered for a specific type of item. This is useful when you have a combo box with a single type of item, and you want to render the items according to your specific needs.
An example of how to use this method is as follows:UI.comboBox(TimeUnit.values()) .withCell( cell -> cell .updateView( comp -> comp .orGet(JLabel::new) .updateIf(JLabel.class, label -> { label.setText(cell.valueAsString().orElse("")); return label; }) ) );
- Type Parameters:
V
- The type of the value that is being rendered in this combo box.- Parameters:
cellConfigurator
- The configurator for the cell, receiving aCellConf
as input and returning an updated CellDelegate.- Returns:
- This very instance, which enables builder-style method chaining.
-
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 aCellConf
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.
-