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.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
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
SelectBox.SelectBoxScrollPane<T>
The scroll pane shown when a select box is open.static class
SelectBox.SelectBoxStyle
The style for a select box, seeSelectBox
.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
clearItems()
void
draw(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 GlyphLayout
drawItem(Batch batch, BitmapFont font, T item, float x, float y, float width)
protected Drawable
getBackgroundDrawable()
Returns appropriate background drawable from the style based on the current select box state.ClickListener
getClickListener()
protected Color
getFontColor()
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.int
getMaxListCount()
float
getMaxSelectedPrefWidth()
Returns the pref width of the select box if the widest item was selected, for use whensetSelectedPrefWidth(boolean)
is true.float
getPrefHeight()
float
getPrefWidth()
SelectBox.SelectBoxScrollPane
getScrollPane()
Returns the scroll pane containing the list that is shown when the select box is open.T
getSelected()
Returns the first selected item, or null.int
getSelectedIndex()
boolean
getSelectedPrefWidth()
ArraySelection<T>
getSelection()
Get the set of selected items, useful when multiple items are selectedSelectBox.SelectBoxStyle
getStyle()
Returns the select box's style.void
hideList()
Deprecated.UsehideScrollPane()
.void
hideScrollPane()
boolean
isDisabled()
boolean
isOver()
void
layout()
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 void
onHide(Actor scrollPane)
protected void
onShow(Actor scrollPane, boolean below)
void
setAlignment(int alignment)
Sets the alignment of the selected item in the select box.void
setDisabled(boolean disabled)
void
setItems(Array<T> newItems)
Sets the items visible in the select box.void
setItems(T... newItems)
Set the backing Array that makes up the choices available in the SelectBoxvoid
setMaxListCount(int maxListCount)
Set the max number of items to display when the select box is opened.void
setScrollingDisabled(boolean y)
Disables scrolling of the list shown when the select box is open.void
setSelected(T item)
Sets the selection to only the passed item, if it is a possible choice, else selects the first item.void
setSelectedIndex(int index)
Sets the selection to only the selected index.void
setSelectedPrefWidth(boolean selectedPrefWidth)
When true the pref width is based on the selected item.protected void
setStage(Stage stage)
Called by the framework when this actor or any ascendant is added to a group that is in the stage.void
setStyle(SelectBox.SelectBoxStyle style)
void
showList()
Deprecated.UseshowScrollPane()
.void
showScrollPane()
protected java.lang.String
toString(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:Actor
Called 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:Layout
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. 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:Widget
If 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:
setDisabled
in interfaceDisableable
-
isDisabled
public boolean isDisabled()
- Specified by:
isDisabled
in interfaceDisableable
-
getPrefWidth
public float getPrefWidth()
- Specified by:
getPrefWidth
in interfaceLayout
- Overrides:
getPrefWidth
in classWidget
-
getPrefHeight
public float getPrefHeight()
- Specified by:
getPrefHeight
in interfaceLayout
- Overrides:
getPrefHeight
in 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)
-
-