Package swingtree

Class UIForCombo<E,C extends JComboBox<E>>


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 Details

    • getComboBox

      public JComboBox<E> getComboBox()
      Builds and returns the configured JComboBox instance.
      Returns:
      The configured JComboBox instance.
    • _state

      protected swingtree.BuilderState<JComboBox<E>> _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 current EventProcessor.
      Specified by:
      _state in class UIForAnything<UIForCombo<E,C extends JComboBox<E>>,JComboBox<E>,JComponent>
      Returns:
      The state of the builder.
    • _newBuilderWithState

      protected UIForCombo<E,C> _newBuilderWithState(swingtree.BuilderState<JComboBox<E>> newState)
      Description copied from class: UIForAnything
      An internal wither method which creates a new builder instance with the provided BuilderState stored inside it.
      Specified by:
      _newBuilderWithState in class UIForAnything<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

      public 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.
      Parameters:
      action - the action to be executed when the combo box is opened.
      Returns:
      this
    • onClose

      public 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.
      Parameters:
      action - the action to be executed when the combo box is closed.
      Returns:
      this
    • onCancel

      public 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.
      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 an Action to the underlying JComboBox through an ActionListener, which will be called when a selection has been made. If the combo box is editable, then an ActionEvent will be fired when editing has stopped. For more information see JComboBox.addActionListener(ActionListener).
      Parameters:
      action - The Action that will be notified.
      Returns:
      This very instance, which enables builder-style method chaining.
      Throws:
      IllegalArgumentException - if action is null.
    • onEnter

      public UIForCombo<E,C> onEnter(sprouts.Action<ComponentDelegate<C,ActionEvent>> action)
      Adds an ActionListener to the editor component of the underlying JComboBox which will be called when a selection has been made. If the combo box is editable, then an ActionEvent will be fired when editing has stopped. For more information see JComboBox.addActionListener(ActionListener).

      Parameters:
      action - The Action that will be notified.
      Returns:
      This very builder instance, which allows for method chaining.
    • isEditableIf

      public UIForCombo<E,C> isEditableIf(boolean isEditable)
      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

      public UIForCombo<E,C> isEditableIf(sprouts.Var<Boolean> isEditable)
      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 - if isEditable is null.
    • _withRendererAndEditor

      public final <V extends E> UIForCombo<E,C> _withRendererAndEditor(CellBuilder<C,V> cellBuilder)
    • withCells

      public 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. You would typically want to use this method to render generic types where the only common type is Object, yet you still want to render the items in a specific way depending on their actual type.
      This is done like so:
      
        UI.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() )
        );
        
      Note that inside the lambda function, you can use the CellBuilder to define for what type of item you want to render the item in a specific way and the RenderAs to define how the item should be rendered.

      You may want to know that a similar API is also available for the JList and JTable components, see UIForList.withCells(Configurator), UIForTable.withCells(Configurator) and UIFactoryMethods.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

      public 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. 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 a CellConf as input and returning an updated CellDelegate.
      Returns:
      This very instance, which enables builder-style method chaining.
    • withCellRenderer

      public final UIForCombo<E,C> withCellRenderer(ListCellRenderer<E> renderer)
      Sets the ListCellRenderer for the JComboBox, which renders the combo box items by supplying a custom component for each item through the ListCellRenderer.getListCellRendererComponent(JList, Object, int, boolean, boolean) method.

      Parameters:
      renderer - The ListCellRenderer that will be used to paint each cell in the combo box.
      Returns:
      This very instance, which enables builder-style method chaining.
    • withTextRenderer

      public final UIForCombo<E,C> withTextRenderer(Function<CellConf<C,E>,String> renderer)
      Use this to specify a custom text based cell renderer for each item in the combo box. The renderer is a function that takes a CellConf as input and returns a String 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

      public 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.
      Parameters:
      model - The ComboBoxModel to be used for modelling the content data of the combo box.
      Returns:
      This very instance, which enables builder-style method chaining.
    • withItems

      public final UIForCombo<E,C> withItems(List<E> options)
      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 - if options is null.
    • withItems

      public final UIForCombo<E,C> withItems(sprouts.Var<E> selection, List<E> options)
      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 the Var 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 - if selection or options is null.
    • withItems

      public final UIForCombo<E,C> withItems(sprouts.Vars<E> options)
      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 - if options is null.
    • withItems

      public final UIForCombo<E,C> withItems(sprouts.Vals<E> options)
      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 - if options is null.
    • withItems

      public final UIForCombo<E,C> withItems(sprouts.Var<E> selection, sprouts.Vars<E> options)
      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 of selection or options is null.
    • withItems

      public final UIForCombo<E,C> withItems(sprouts.Var<E> selection, sprouts.Vals<E> options)
      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 of selection or options is null.
    • withItems

      @SafeVarargs public final UIForCombo<E,C> withItems(sprouts.Var<E> selection, E... options)
      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 of selection or options is null.
    • withItems

      public final UIForCombo<E,C> withItems(sprouts.Var<E> selection, sprouts.Var<E[]> options)
      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 of selection or options is null.
    • withItems

      public final UIForCombo<E,C> withItems(sprouts.Var<E> selection, sprouts.Val<E[]> options)
      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 of selection or options is null.
    • withSelectedItem

      public final UIForCombo<E,C> withSelectedItem(E item)
      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

      public final UIForCombo<E,C> withSelectedItem(sprouts.Var<E> item)
      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.