Class SelectBox<T>

  • All Implemented Interfaces:
    Disableable, Layout

    public class SelectBox<T>
    extends Widget
    implements Disableable
    A select box (aka a drop-down list) allows a user to choose one of a number of values from a list. When inactive, the selected value is displayed. When activated, it shows the list of values that may be selected.

    ChangeListener.ChangeEvent is fired when the selectbox selection changes.

    The preferred size of the select box is determined by the maximum text bounds of the items and the size of the SelectBox.SelectBoxStyle.background.

    • Constructor Detail

      • SelectBox

        public SelectBox​(Skin skin)
      • SelectBox

        public SelectBox​(Skin skin,
                         java.lang.String styleName)
    • Method Detail

      • newScrollPane

        protected SelectBox.SelectBoxScrollPane<T> newScrollPane()
        Allows a subclass to customize the scroll pane shown when the select box is open.
      • setMaxListCount

        public void setMaxListCount​(int maxListCount)
        Set the max number of items to display when the select box is opened. Set to 0 (the default) to display as many as fit in the stage height.
      • getMaxListCount

        public int getMaxListCount()
        Returns:
        Max number of items to display when the box is opened, or <= 0 to display them all.
      • setStage

        protected void setStage​(Stage stage)
        Description copied from class: Actor
        Called by the framework when this actor or any ascendant is added to a group that is in the stage.
        Overrides:
        setStage in class Actor
        Parameters:
        stage - May be null if the actor or any ascendant is no longer in a stage.
      • setItems

        public void setItems​(T... newItems)
        Set the backing Array that makes up the choices available in the SelectBox
      • setItems

        public void setItems​(Array<T> newItems)
        Sets the items visible in the select box.
      • clearItems

        public void clearItems()
      • getItems

        public Array<T> getItems()
        Returns the internal items array. If modified, setItems(Array) must be called to reflect the changes.
      • layout

        public void layout()
        Description copied from interface: Layout
        Computes and caches any information needed for drawing and, if this actor has children, positions and sizes each child, calls Layout.invalidate() on any each child whose width or height has changed, and calls Layout.validate() on each child. This method should almost never be called directly, instead Layout.validate() should be used.
        Specified by:
        layout in interface Layout
        Overrides:
        layout in class Widget
      • getBackgroundDrawable

        @Null
        protected Drawable getBackgroundDrawable()
        Returns appropriate background drawable from the style based on the current select box state.
      • getFontColor

        protected Color getFontColor()
        Returns the appropriate label font color from the style based on the current button state.
      • draw

        public void draw​(Batch batch,
                         float parentAlpha)
        Description copied from class: Widget
        If this method is overridden, the super method or Widget.validate() should be called to ensure the widget is laid out.
        Overrides:
        draw in class Widget
        parentAlpha - The parent alpha, to be multiplied with this actor's alpha, allowing the parent's alpha to affect all children.
      • setAlignment

        public void setAlignment​(int alignment)
        Sets the alignment of the selected item in the select box. See getList() and List.setAlignment(int) to set the alignment in the list shown when the select box is open.
        Parameters:
        alignment - See Align.
      • getSelection

        public ArraySelection<T> getSelection()
        Get the set of selected items, useful when multiple items are selected
        Returns:
        a Selection object containing the selected elements
      • getSelected

        @Null
        public T getSelected()
        Returns the first selected item, or null. For multiple selections use getSelection().
      • setSelected

        public void setSelected​(@Null
                                T item)
        Sets the selection to only the passed item, if it is a possible choice, else selects the first item.
      • getSelectedIndex

        public int getSelectedIndex()
        Returns:
        The index of the first selected item. The top item has an index of 0. Nothing selected has an index of -1.
      • setSelectedIndex

        public void setSelectedIndex​(int index)
        Sets the selection to only the selected index.
      • setSelectedPrefWidth

        public void setSelectedPrefWidth​(boolean selectedPrefWidth)
        When true the pref width is based on the selected item.
      • getSelectedPrefWidth

        public boolean getSelectedPrefWidth()
      • getMaxSelectedPrefWidth

        public float getMaxSelectedPrefWidth()
        Returns the pref width of the select box if the widest item was selected, for use when setSelectedPrefWidth(boolean) is true.
      • setDisabled

        public void setDisabled​(boolean disabled)
        Specified by:
        setDisabled in interface Disableable
      • toString

        protected java.lang.String toString​(T item)
      • showList

        @Deprecated
        public void showList()
        Deprecated.
      • showScrollPane

        public void showScrollPane()
      • hideList

        @Deprecated
        public void hideList()
        Deprecated.
      • hideScrollPane

        public void hideScrollPane()
      • getList

        public List<T> getList()
        Returns the list shown when the select box is open.
      • setScrollingDisabled

        public void setScrollingDisabled​(boolean y)
        Disables scrolling of the list shown when the select box is open.
      • getScrollPane

        public SelectBox.SelectBoxScrollPane getScrollPane()
        Returns the scroll pane containing the list that is shown when the select box is open.
      • isOver

        public boolean isOver()
      • onShow

        protected void onShow​(Actor scrollPane,
                              boolean below)
      • onHide

        protected void onHide​(Actor scrollPane)