libgdx API

com.badlogic.gdx.scenes.scene2d.ui
Class ComboBox

java.lang.Object
  extended by com.badlogic.gdx.scenes.scene2d.Actor
      extended by com.badlogic.gdx.scenes.scene2d.ui.Widget
          extended by com.badlogic.gdx.scenes.scene2d.ui.ComboBox
All Implemented Interfaces:
Layout

public class ComboBox
extends Widget

A dropdown or combo box.

Functionality

A ComboBox contains a list of Strings, with one of the strings being selected and displayed in the main area of the ComboBox. Clicking the ComboBox brings up a popup list showing all the items. This popup list will grab the touch focus while it is displayed. This is achieved by temporarily adding a new Actor to the root of the Stage the ComboBox is contained in. As soon as an item is selected or a mouse click outside the area of the popup list is registered, the popup will disappear again and the focus is given back.

A ComboBox.SelectionListener can be registered with the ComboBox to receive notification of selection changes.

Layout

A ComboBox's (preferred) width and height are determined by the border patches in the background NinePatch as well as the bounding box of the widest item in the list of strings. Use ComboBox#setPrefSize(int, int) to change this size programmatically. In case the set size is to small to contain the widest item, artifacts may appear.

The additional popup list will be positioned at the bottom edge of the ComboBox, displaying all items. The width and size is governed by the background NinePatch of the popup list as well as the bounding box around the list items.

Style

A ComboBox is a Widget displaying a background NinePatch as well as the selected list item as a label via a BitmapFont and a corresponding Color. Additionally a popup menu might be displayed, using a NinePatch for the background, another NinePatch for highlighting the current selection and the same BitmapFont and Color used to display the selected item in the actual ComboBox.

The style is defined via an instance of the ComboBox.ComboBoxStyle class, which can be either done programmatically or via a Skin.

A ComboBox's style definition in a skin XML file should look like this:
 <combobox name="styleName"  			 
           background="backgroundNinePatch" 
           listBackground="popupBackgroundNinePatch" 
           listSelection="popupSelectionNinePatch"
           font="fontName" 
           fontColor="colorName" />
 
 

Author:
mzechner

Nested Class Summary
static class ComboBox.ComboBoxStyle
          Defines the style of a combo box.
protected  class ComboBox.ComboList
           
static interface ComboBox.SelectionListener
          Interface for listening to selection events.
 
Field Summary
 
Fields inherited from class com.badlogic.gdx.scenes.scene2d.ui.Widget
invalidated
 
Fields inherited from class com.badlogic.gdx.scenes.scene2d.Actor
actions, color, height, name, originX, originY, parent, rotation, scaleX, scaleY, touchable, visible, width, x, y
 
Constructor Summary
ComboBox(java.lang.String[] items, Stage stage, ComboBox.ComboBoxStyle style)
           
ComboBox(java.lang.String[] items, Stage stage, ComboBox.ComboBoxStyle style, java.lang.String name)
          Creates a new combo box.
ComboBox(java.lang.String[] items, Stage stage, Skin skin)
           
 
Method Summary
 void draw(SpriteBatch batch, float parentAlpha)
          Draws the Actor.
 float getPrefHeight()
           
 float getPrefWidth()
           
 java.lang.String getSelection()
           
 int getSelectionIndex()
           
 void layout()
          Positions and sizes each child of this actor.
 void setItems(java.lang.String[] items)
           
 void setSelection(int selection)
          Sets the selected item via it's index
 void setSelectionListener(ComboBox.SelectionListener listener)
          Sets the ComboBox.SelectionListener.
 void setStyle(ComboBox.ComboBoxStyle style)
          Sets the style of this widget.
 boolean touchDown(float x, float y, int pointer)
           
 void touchDragged(float x, float y, int pointer)
           
 void touchUp(float x, float y, int pointer)
           
 
Methods inherited from class com.badlogic.gdx.scenes.scene2d.ui.Widget
getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, hit, invalidate, invalidateHierarchy
 
Methods inherited from class com.badlogic.gdx.scenes.scene2d.Actor
act, action, clearActions, isMarkedToRemove, keyDown, keyTyped, keyUp, markToRemove, remove, scrolled, toLocalCoordinates, toString, touchMoved
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ComboBox

public ComboBox(java.lang.String[] items,
                Stage stage,
                Skin skin)

ComboBox

public ComboBox(java.lang.String[] items,
                Stage stage,
                ComboBox.ComboBoxStyle style)

ComboBox

public ComboBox(java.lang.String[] items,
                Stage stage,
                ComboBox.ComboBoxStyle style,
                java.lang.String name)
Creates a new combo box. The width and height are determined by the widets item and the style.

Parameters:
name - the name
items - the single-line items
stage - the stage, used for the popup
style - the ComboBox.ComboBoxStyle
Method Detail

setStyle

public void setStyle(ComboBox.ComboBoxStyle style)
Sets the style of this widget.

Parameters:
style -

setItems

public void setItems(java.lang.String[] items)

layout

public void layout()
Description copied from interface: Layout
Positions and sizes each child of this actor. Subsequent calls will not have any affect unless Layout.invalidate() is called.


draw

public void draw(SpriteBatch batch,
                 float parentAlpha)
Description copied from class: Actor
Draws the Actor. The spriteBatch is configured so that the Actor can draw in its parents coordinate system. The parent's alpha is passed to the method in order for the Actor to multiply it with its own alpha. This will allow FadeIn and other Actions to have an effect even if they are only set on the parent of the Actor.

Specified by:
draw in class Actor
Parameters:
batch - the spritebatch to render with
parentAlpha - the parent's alpha value.

touchDown

public boolean touchDown(float x,
                         float y,
                         int pointer)
Specified by:
touchDown in class Actor

touchUp

public void touchUp(float x,
                    float y,
                    int pointer)
Specified by:
touchUp in class Actor

touchDragged

public void touchDragged(float x,
                         float y,
                         int pointer)
Specified by:
touchDragged in class Actor

setSelectionListener

public void setSelectionListener(ComboBox.SelectionListener listener)
Sets the ComboBox.SelectionListener.

Parameters:
listener - the listener or null

setSelection

public void setSelection(int selection)
Sets the selected item via it's index

Parameters:
selection - the selection index

getSelectionIndex

public int getSelectionIndex()
Returns:
the index of the current selection. The top item has an index of 0

getSelection

public java.lang.String getSelection()
Returns:
the string of the currently selected item

getPrefWidth

public float getPrefWidth()

getPrefHeight

public float getPrefHeight()

libgdx API

Copyright 2010 Mario Zechner (contact@badlogicgames.com), Nathan Sweet (admin@esotericsoftware.com)