Package swingtree

Class SplitItem<I extends JMenuItem>

java.lang.Object
swingtree.SplitItem<I>
Type Parameters:
I - The type of the item which will be passed to the Actions.

public final class SplitItem<I extends JMenuItem> extends Object
An immutable data carrier exposing everything needed to configure an item of a JSplitButton. SplitItems will be turned into button options for the JSplitButton which can be supplied to a split button through the builder API exposed by UIForSplitButton like so:

      UI.splitButton("Hey!")
      .add(UI.splitItem("first"))
      .add(UI.splitItem("second").onClick( it -> ... ))
      .add(UI.splitItem("third").onClick( it -> ... ).onSelected( it -> ... ))
  

For more information, 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

    • of

      public static SplitItem<JMenuItem> of(String text)
      A factory method to create a SplitItem for a JSplitButton from a simple text string which will be displayed on the SplitItem when it is part of a clicked JSplitButton.
      Parameters:
      text - The text which should be displayed on the SplitItem (and its underlying JMenuItem).
      Returns:
      A SplitItem wrapping a simple JMenuItem displaying the provided text.
    • of

      public static SplitItem<JMenuItem> of(sprouts.Val<String> text)
      A factory method to create a SplitItem for a JSplitButton from a Val property which will be used to dynamically determine the text displayed on the SplitItem.
      Parameters:
      text - The text property whose text should be displayed on the SplitItem (and its underlying JMenuItem). When the text of the property changes, then the text of the JMenuItem will be updated accordingly.
      Returns:
      A SplitItem wrapping a simple JMenuItem displaying the provided text.
    • of

      public static <I extends JMenuItem> SplitItem<I> of(I item)
      A factory method to create a SplitItem for a JSplitButton from a JMenuItem subtype.
      Type Parameters:
      I - The type parameter for the provided item type.
      Parameters:
      item - The JMenuItem subtype for which a SplitItem (for JSplitButton) should be created.
      Returns:
      A SplitItem wrapping the provided JMenuItem type.
    • of

      public static <M extends JMenuItem> SplitItem<M> of(UIForMenuItem<M> item)
      Use this to create a SplitItem for a JSplitButton from a UIForMenuItem UI declaration. This is useful when you want to create a SplitItem from a JMenuItem which is configured using the declarative UI builder API exposed by UIForMenuItem. See UIFactoryMethods.menuItem() for more information.
      Type Parameters:
      M - The type parameter for the provided item type, a subtype of JMenuItem.
      Parameters:
      item - The UIForMenuItem which wraps a JMenuItem for which a SplitItem should be created.
      Returns:
      A SplitItem wrapping JMenuItem represented by the provided UI builder.
    • makeSelected

      public SplitItem<I> makeSelected()
      Sets the AbstractButton.setSelected(boolean) flag of the underlying JMenuItem to true.
      Returns:
      An immutable copy of this with the provided text set.
    • onButtonClick

      public SplitItem<I> onButtonClick(sprouts.Action<SplitItemDelegate<I>> action)
      Use this to register an action which will be called when the JSplitButton is being pressed and this SplitItem was selected to be the primary button.
      
            UI.splitButton("Hey!")
            .add(UI.splitItem("load"))
            .add(UI.splitItem("save").onClick( it -> doSaving() ))
            .add(UI.splitItem("delete"))
        
      Parameters:
      action - The action lambda which will be called when the JSplitButton is being pressed and this SplitItem was selected.
      Returns:
      An immutable copy of this with the provided lambda set.
    • onSelection

      public SplitItem<I> onSelection(sprouts.Action<SplitItemDelegate<I>> action)
      Use this to perform some action when the user selects a SplitItem among all other split button items. A common use case would be to set the text of the JSplitButton by calling the SplitItemDelegate.getSplitButton() method on the context object supplied to the provided action lambda like so:
      
            UI.splitButton("Hey!")
            .add(UI.splitItem("first"))
            .add(UI.splitItem("second").onSelected( it -> it.getSplitButton().setText("Hey hey!") ))
            .add(UI.splitItem("third"))
        
      Parameters:
      action - The action which will be called when the button was selected and which will receive some context information in the form of a SplitItemDelegate instance.
      Returns:
      An immutable copy of this with the provided lambda set.
    • isEnabledIf

      public SplitItem<I> isEnabledIf(sprouts.Var<Boolean> isEnabled)
      Dynamically determines whether this SplitItem is enabled or not based on the value of the provided observable boolean property. This means that whenever the value of the property changes, the enabled state of this SplitItem will change accordingly. This is done by calling JMenuItem.setEnabled(boolean) on the underlying JMenuItem with the value of the property.
      Parameters:
      isEnabled - An observable boolean property which will dynamically determine whether this SplitItem is enabled or not. So when the property changes, the enabled state of this SplitItem will be updated accordingly.
      Returns:
      An immutable copy of this with the provided property set.