Class SelectBox<T>
- java.lang.Object
-
- com.badlogic.gdx.scenes.scene2d.Actor
-
- com.badlogic.gdx.scenes.scene2d.ui.Widget
-
- com.badlogic.gdx.scenes.scene2d.ui.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.ChangeEventis 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSelectBox.SelectBoxScrollPane<T>The scroll pane shown when a select box is open.static classSelectBox.SelectBoxStyleThe style for a select box, seeSelectBox.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidclearItems()voiddraw(Batch batch, float parentAlpha)If this method is overridden, the super method orWidget.validate()should be called to ensure the widget is laid out.protected GlyphLayoutdrawItem(Batch batch, BitmapFont font, T item, float x, float y, float width)protected DrawablegetBackgroundDrawable()Returns appropriate background drawable from the style based on the current select box state.ClickListenergetClickListener()protected ColorgetFontColor()Returns the appropriate label font color from the style based on the current button state.Array<T>getItems()Returns the internal items array.List<T>getList()Returns the list shown when the select box is open.intgetMaxListCount()floatgetMaxSelectedPrefWidth()Returns the pref width of the select box if the widest item was selected, for use whensetSelectedPrefWidth(boolean)is true.floatgetPrefHeight()floatgetPrefWidth()SelectBox.SelectBoxScrollPanegetScrollPane()Returns the scroll pane containing the list that is shown when the select box is open.TgetSelected()Returns the first selected item, or null.intgetSelectedIndex()booleangetSelectedPrefWidth()ArraySelection<T>getSelection()Get the set of selected items, useful when multiple items are selectedSelectBox.SelectBoxStylegetStyle()Returns the select box's style.voidhideList()Deprecated.UsehideScrollPane().voidhideScrollPane()booleanisDisabled()booleanisOver()voidlayout()Computes and caches any information needed for drawing and, if this actor has children, positions and sizes each child, callsLayout.invalidate()on any each child whose width or height has changed, and callsLayout.validate()on each child.protected SelectBox.SelectBoxScrollPane<T>newScrollPane()Allows a subclass to customize the scroll pane shown when the select box is open.protected voidonHide(Actor scrollPane)protected voidonShow(Actor scrollPane, boolean below)voidsetAlignment(int alignment)Sets the alignment of the selected item in the select box.voidsetDisabled(boolean disabled)voidsetItems(Array<T> newItems)Sets the items visible in the select box.voidsetItems(T... newItems)Set the backing Array that makes up the choices available in the SelectBoxvoidsetMaxListCount(int maxListCount)Set the max number of items to display when the select box is opened.voidsetScrollingDisabled(boolean y)Disables scrolling of the list shown when the select box is open.voidsetSelected(T item)Sets the selection to only the passed item, if it is a possible choice, else selects the first item.voidsetSelectedIndex(int index)Sets the selection to only the selected index.voidsetSelectedPrefWidth(boolean selectedPrefWidth)When true the pref width is based on the selected item.protected voidsetStage(Stage stage)Called by the framework when this actor or any ascendant is added to a group that is in the stage.voidsetStyle(SelectBox.SelectBoxStyle style)voidshowList()Deprecated.UseshowScrollPane().voidshowScrollPane()protected java.lang.StringtoString(T item)-
Methods inherited from class com.badlogic.gdx.scenes.scene2d.ui.Widget
getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, invalidate, invalidateHierarchy, needsLayout, pack, setFillParent, setLayoutEnabled, sizeChanged, validate
-
Methods inherited from class com.badlogic.gdx.scenes.scene2d.Actor
act, addAction, addCaptureListener, addListener, ancestorsVisible, ascendantsVisible, clear, clearActions, clearListeners, clipBegin, clipBegin, clipEnd, debug, drawDebug, drawDebugBounds, fire, firstAscendant, getActions, getCaptureListeners, getColor, getDebug, getHeight, getListeners, getName, getOriginX, getOriginY, getParent, getRight, getRotation, getScaleX, getScaleY, getStage, getTop, getTouchable, getUserObject, getWidth, getX, getX, getY, getY, getZIndex, hasActions, hasKeyboardFocus, hasParent, hasScrollFocus, hit, isAscendantOf, isDescendantOf, isTouchable, isTouchFocusListener, isTouchFocusTarget, isVisible, localToActorCoordinates, localToAscendantCoordinates, localToParentCoordinates, localToScreenCoordinates, localToStageCoordinates, moveBy, notify, parentToLocalCoordinates, positionChanged, remove, removeAction, removeCaptureListener, removeListener, rotateBy, rotationChanged, scaleBy, scaleBy, scaleChanged, screenToLocalCoordinates, setBounds, setColor, setColor, setDebug, setHeight, setName, setOrigin, setOrigin, setOriginX, setOriginY, setParent, setPosition, setPosition, setRotation, setScale, setScale, setScaleX, setScaleY, setSize, setTouchable, setUserObject, setVisible, setWidth, setX, setX, setY, setY, setZIndex, sizeBy, sizeBy, stageToLocalCoordinates, toBack, toFront, toString
-
-
-
-
Constructor Detail
-
SelectBox
public SelectBox(Skin skin)
-
SelectBox
public SelectBox(Skin skin, java.lang.String styleName)
-
SelectBox
public SelectBox(SelectBox.SelectBoxStyle style)
-
-
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:ActorCalled by the framework when this actor or any ascendant is added to a group that is in the stage.
-
setStyle
public void setStyle(SelectBox.SelectBoxStyle style)
-
getStyle
public SelectBox.SelectBoxStyle getStyle()
Returns the select box's style. Modifying the returned style may not have an effect untilsetStyle(SelectBoxStyle)is called.
-
setItems
public void setItems(T... newItems)
Set the backing Array that makes up the choices available in the SelectBox
-
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:LayoutComputes and caches any information needed for drawing and, if this actor has children, positions and sizes each child, callsLayout.invalidate()on any each child whose width or height has changed, and callsLayout.validate()on each child. This method should almost never be called directly, insteadLayout.validate()should be used.
-
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:WidgetIf this method is overridden, the super method orWidget.validate()should be called to ensure the widget is laid out.
-
drawItem
protected GlyphLayout drawItem(Batch batch, BitmapFont font, T item, float x, float y, float width)
-
setAlignment
public void setAlignment(int alignment)
Sets the alignment of the selected item in the select box. SeegetList()andList.setAlignment(int)to set the alignment in the list shown when the select box is open.- Parameters:
alignment- SeeAlign.
-
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 usegetSelection().
-
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 whensetSelectedPrefWidth(boolean)is true.
-
setDisabled
public void setDisabled(boolean disabled)
- Specified by:
setDisabledin interfaceDisableable
-
isDisabled
public boolean isDisabled()
- Specified by:
isDisabledin interfaceDisableable
-
getPrefWidth
public float getPrefWidth()
- Specified by:
getPrefWidthin interfaceLayout- Overrides:
getPrefWidthin classWidget
-
getPrefHeight
public float getPrefHeight()
- Specified by:
getPrefHeightin interfaceLayout- Overrides:
getPrefHeightin classWidget
-
toString
protected java.lang.String toString(T item)
-
showList
@Deprecated public void showList()
Deprecated.UseshowScrollPane().
-
showScrollPane
public void showScrollPane()
-
hideList
@Deprecated public void hideList()
Deprecated.UsehideScrollPane().
-
hideScrollPane
public void hideScrollPane()
-
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()
-
getClickListener
public ClickListener getClickListener()
-
onShow
protected void onShow(Actor scrollPane, boolean below)
-
onHide
protected void onHide(Actor scrollPane)
-
-