public class ComboBox<T>
ImplementsActionSource, Animation, Editable, StyleListener
A ComboBox is a list that allows only one selection at a time, when a user clicks
the ComboBox a popup button with the full list of elements allows the selection of
a single element. The ComboBox is driven by the list model and allows all the renderer
features of the List as well.
The ComboBox is notoriously hard to style properly as it relies on a complex dynamic of
popup renderer and instantly visible renderer. The UIID for the ComboBox is “ComboBox”
however if you set it to something else all the other UIID’s will also change their prefix. E.g. the “ComboBoxPopup”
UIID will become “MyNewUIIDPopup”.
The combo box defines the following UIID’s by default:
ComboBoxComboBoxItemComboBoxFocusPopupContentPanePopupItemPopupFocus
This class also defines theme constants that allow some native themes to manipulate its behavior e.g.:
popupTitleBool - shows the “label for” value as the title of the popup dialog
popupCancelBodyBool - Adds a cancel button into the popup dialog
centeredPopupBool - shows the popup dialog in the center of the screen instead of under the popup
otherPopupRendererBool - Uses a different list cell render for the popup than the one used for the
ComboBoxitself. When this is falsePopupItem&PopupFocusbecome irrelevant. Notice that the Android native theme defines this to true.
iOS doesn’t use combo boxes as part of its UI paradigm. Its available there mostly in web applications and feels
unnatural in iOS which is why we recommend using the com.codename1.ui.spinner.Picker class.
The sample code below uses the com.codename1.ui.list.GenericListCellRenderer to create a richer
ComboBox UI.
public void showForm() {
Form hi = new Form("ComboBox", new BoxLayout(BoxLayout.Y_AXIS));
ComboBox> combo = new ComboBox<> (
createListEntry("A Game of Thrones", "1996"),
createListEntry("A Clash Of Kings", "1998"),
createListEntry("A Storm Of Swords", "2000"),
createListEntry("A Feast For Crows", "2005"),
createListEntry("A Dance With Dragons", "2011"),
createListEntry("The Winds of Winter", "2016 (please, please, please)"),
createListEntry("A Dream of Spring", "Ugh"));
combo.setRenderer(new GenericListCellRenderer<>(new MultiButton(), new MultiButton()));
hi.show();
}
private Map createListEntry(String name, String date) {
Map entry = new HashMap<>();
entry.put("Line1", name);
entry.put("Line2", date);
return entry;
}
Fields
public static final int POPUP_PLACEMENT_AUTO = 0 | Popup placement: place the list adjacent to the combo box (default behavior: above when the combo sits in the lower half of the form, below otherwise). |
public static final int POPUP_PLACEMENT_ABOVE = 1 | Popup placement: always anchor the list directly above the combo box. |
public static final int POPUP_PLACEMENT_BELOW = 2 | Popup placement: always anchor the list directly below the combo box. |
public static final int POPUP_PLACEMENT_TOP_OF_FORM = 3 | Popup placement: pin the list to the top edge of the form. |
public static final int POPUP_PLACEMENT_BOTTOM_OF_FORM = 4 | Popup placement: pin the list to the bottom edge of the form. |
Constructors
public ComboBox(Vector<T> items) | Creates a new instance of ComboBox |
public ComboBox(Object... items) | Creates a new instance of ComboBox |
public ComboBox() | Constructs an empty combo box |
public ComboBox(ListModel<T> model) | Creates a new instance of ComboBox |
Methods
public static boolean isDefaultActAsSpinnerDialog() | When this flag is active the combo box acts as a button that opens a dialog that looks like a spinner this allows creating user interfaces for touch devices where a spinner UI approach is more common than a combo box paradigm. |
public static void setDefaultActAsSpinnerDialog(boolean aDefaultActAsSpinnerDialog) | When this flag is active the combo box acts as a button that opens a dialog that looks like a spinner this allows creating user interfaces for touch devices where a spinner UI approach is more common than a combo box paradigm. |
public static boolean isDefaultIncludeSelectCancel() | Indicates whethe the soft buttons for select/cancel should appear for the combo box by default |
public static void setDefaultIncludeSelectCancel(boolean aDefaultIncludeSelectCancel) | Indicates whethe the soft buttons for select/cancel should appear for the combo box by default |
public Image getComboBoxImage() | Gets the ComboBox drop down icon |
public void setComboBoxImage(Image comboBoxImage) | Sets the ComboBox drop down icon |
public void setUIID(String uiid) | This method sets the Component the Unique identifier. |
public int getBaseline(int width, int height) | The baseline for the component text according to which it should be aligned with other components for best visual look. |
protected void laidOut() | This is a callback method to inform the Component when it’s been laidout on the parent Container |
public Rectangle getSelectedRect() | Returns the component bounds with absolute screen coordinates, for components that include an internal selection behavior and are not containers (currently only List) this method allows returning the position of the selection itself which i… |
protected Rectangle getVisibleBounds() | Returns the component bounds for scrolling which might differ from the getBounds for large components e.g. list. |
public void setSelectedIndex(int selection) | Sets the current selected offset in the list, by default this implementation will scroll the list to the selection if the selection is outside of the screen |
public void setSelectedIndex(int selection, boolean scroll) | Sets the current selected offset in the list |
public void pointerHover(int[] x, int[] y) | Invoked for devices where the pointer can hover without actually clicking the display. |
public void pointerHoverReleased(int[] x, int[] y) | Invoked for devices where the pointer can hover without actually clicking the display. |
protected Dialog createPopupDialog(List<T> l) | Subclasses can override this method to change the creation of the dialog |
public boolean isShowingPopupDialog() | Returns true if the popup dialog is currently showing for this combobox. |
protected Command showPopupDialog(Dialog popupDialog, List l) | Shows the popup dialog for the combo box and returns the resulting command. |
protected void fireClicked() | When working in 3 softbutton mode “fire” key (center softbutton) is sent to this method in order to allow 3 button devices to work properly. |
protected List<T> createPopupList() | Creates the list object used within the popup dialog. |
public void keyReleased(int keyCode) | If this Component is focused, the key released event will call this method |
public void pointerPressed(int x, int y) | If this Component is focused, the pointer pressed event will call this method |
public void pointerDragged(int x, int y) | If this Component is focused, the pointer dragged event will call this method |
public void pointerReleased(int x, int y) | If this Component is focused, the pointer released event will call this method |
public void paint(Graphics g) | This method paints the Component on the screen, it should be overriden by subclasses to perform custom drawing or invoke the UI API’s to let the PLAF perform the rendering. |
protected Dimension calcPreferredSize() | Calculates the preferred size based on component content. |
public int getOrientation() | Returns the list orientation |
public boolean isIncludeSelectCancel() | Indicates whethe the soft buttons for select/cancel should appear for the combo box |
public void setIncludeSelectCancel(boolean includeSelectCancel) | Indicates whethe the soft buttons for select/cancel should appear for the combo box |
public boolean isActAsSpinnerDialog() | When this flag is active the combo box acts as a button that opens a dialog that looks like a spinner this allows creating user interfaces for touch devices where a spinner UI approach is more common than a combo box paradigm. |
public void setActAsSpinnerDialog(boolean actAsSpinnerDialog) | When this flag is active the combo box acts as a button that opens a dialog that looks like a spinner this allows creating user interfaces for touch devices where a spinner UI approach is more common than a combo box paradigm. |
public int getPopupPlacement() | Returns the popup placement mode that controls where showPopupDialog anchors the list when the combo is tapped. |
public void setPopupPlacement(int popupPlacement) | Controls where the popup list is anchored relative to the combo box or the parent form when the combo is tapped. |
Inherited fields
From List
FIXED_NONE, FIXED_NONE_CYCLIC, FIXED_NONE_ONE_ELEMENT_MARGIN_FROM_EDGE, FIXED_LEAD, FIXED_TRAIL, FIXED_CENTER, VERTICAL, HORIZONTAL
From Component
DEFAULT_CURSOR, CROSSHAIR_CURSOR, TEXT_CURSOR, WAIT_CURSOR, SW_RESIZE_CURSOR, SE_RESIZE_CURSOR, NW_RESIZE_CURSOR, NE_RESIZE_CURSOR, N_RESIZE_CURSOR, S_RESIZE_CURSOR, W_RESIZE_CURSOR, E_RESIZE_CURSOR, HAND_CURSOR, MOVE_CURSOR, DRAG_REGION_NOT_DRAGGABLE, DRAG_REGION_POSSIBLE_DRAG_X, DRAG_REGION_POSSIBLE_DRAG_Y, DRAG_REGION_POSSIBLE_DRAG_XY, DRAG_REGION_LIKELY_DRAG_X, DRAG_REGION_LIKELY_DRAG_Y, DRAG_REGION_LIKELY_DRAG_XY, DRAG_REGION_IMMEDIATELY_DRAG_X, DRAG_REGION_IMMEDIATELY_DRAG_Y, DRAG_REGION_IMMEDIATELY_DRAG_XY, BRB_CONSTANT_ASCENT, BRB_CONSTANT_DESCENT, BRB_CENTER_OFFSET, BRB_OTHER, CENTER, TOP, LEFT, BOTTOM, RIGHT, BASELINE
Inherited methods
From List
isDefaultIgnoreFocusComponentWhenUnfocused, setDefaultIgnoreFocusComponentWhenUnfocused, isDefaultFireOnClick, setDefaultFireOnClick, initLaf, modelChanged, listSelectionChanged, getSideGap, isScrollableY, isScrollableX, getMaxElementHeight, setMaxElementHeight, getMinElementHeight, setMinElementHeight, size, getAccessibilityItemBounds, getAccessibilityItemText, getAccessibilityVisibleItemIndices, getCurrentSelected, getSelectedIndex, getDragRegionStatus, getSelectedItem, setSelectedItem, getModel, setModel, setShouldCalcPreferredSize, isNumericKeyActions, setNumericKeyActions, isCommandList, setCommandList, isIgnoreFocusComponentWhenUnfocused, setIgnoreFocusComponentWhenUnfocused, isMutableRendererBackgrounds, setMutableRendererBackgrounds, getListSizeCalculationSampleCount, setListSizeCalculationSampleCount, isLongPointerPressActionEnabled, setLongPointerPressActionEnabled, setListCellRenderer, getRenderer, setRenderer, setOrientation, refreshTheme, scrollRectToVisible, setHandlesInput, isSelectableInteraction, keyPressed, shouldRenderSelection, addSelectionListener, removeSelectionListener, addActionListener, getActionListeners, getListeners, removeActionListener, fireActionEvent, fireActionEvent, setInputOnFocus, setPaintFocusBehindList, getItemGap, setItemGap, getRenderingPrototype, setRenderingPrototype, longPointerPress, setFireOnClick, addItem, getFixedSelection, setFixedSelection, animate, isTactileTouch, setScrollToSelected, getGridPosY, getGridPosX, paramString, getHint, setHint, getHintIcon, setHintIcon, setHint
From Component
setSameSize, isSetCursorSupported, parsePreferredSize, getDefaultDragTransparency, setDefaultDragTransparency, getEditingDelegate, setEditingDelegate, getCursor, setCursor, showNativeOverlay, hideNativeOverlay, updateNativeOverlay, getNativeOverlay, getAllStyles, getSameWidth, setSameWidth, getSameHeight, setSameHeight, getUIManager, getX, setX, getOuterX, getInnerX, getY, setY, getOuterY, getInnerY, isVisible, setVisible, getClientProperty, stripMarginAndPadding, clearClientProperties, putClientProperty, getDirtyRegion, setDirtyRegion, isOpaque, setOpaque, getWidth, setWidth, getOuterWidth, getInnerWidth, getHeight, setHeight, getOuterHeight, getInnerHeight, isDragRegion, getBaselineResizeBehavior, getPreferredSizeStr, setPreferredSizeStr, getPreferredSize, setPreferredSize, getScrollDimension, calcScrollSize, setScrollSize, getPreferredW, setPreferredW, getPreferredH, setPreferredH, getOuterPreferredH, getInnerPreferredH, getOuterPreferredW, getInnerPreferredW, setSize, getUIID, setUIIDFinal, setUIID, getInlineAllStyles, setInlineAllStyles, getInlineSelectedStyles, setInlineSelectedStyles, getInlineUnselectedStyles, setInlineUnselectedStyles, getInlineDisabledStyles, setInlineDisabledStyles, getInlinePressedStyles, setInlinePressedStyles, remove, getParent, getOwner, setOwner, isOwnedBy, containsOrOwns, addFocusListener, removeFocusListener, addScrollListener, removeScrollListener, getSelectCommandText, setSelectCommandText, getLabelForComponent, setLabelForComponent, focusGained, focusLost, paintBackgrounds, paintShadows, getAbsoluteX, getAbsoluteY, isInClippingRegion, paintIntersectingComponentsAbove, paintScrollbars, paintScrollbarX, getScrollOpacity, paintScrollbarY, paintComponent, paintComponent, getBorder, getScrollable, paintBackground, isScrollable, getScrollX, setScrollX, getScrollY, setScrollY, onScrollX, onScrollY, getDraggedx, getDraggedy, getBottomGap, contains, visibleBoundsContains, hasFixedPreferredSize, getBounds, getBounds, getVisibleBounds, isFocusable, setFocusable, onSetFocusable, resetFocusable, getTabIndex, setTabIndex, getPreferredTabIndex, setPreferredTabIndex, isTraversable, setTraversable, handlesInput, consumesRawTextInput, hasFocus, setFocus, getComponentForm, getTopLevelContainer, repaint, repaint, longKeyPress, keyRepeated, registerForAnimation, deregisterFromAnimation, getAnimationManager, getScrollAnimationSpeed, setScrollAnimationSpeed, isBlockLead, setBlockLead, isIgnorePointerEvents, setIgnorePointerEvents, isRippleEffect, setRippleEffect, getInlineStylesTheme, setInlineStylesTheme, shouldRenderComponentSelection, isHideInLandscape, setHideInLandscape, createStyleAnimation, isSmoothScrolling, setSmoothScrolling, stopScrollMomentum, pointerHoverPressed, pinch, pinchReleased, pinch, rotation, isPinchBlocksDragAndDrop, setPinchBlocksDragAndDrop, pointerDragged, getDragImage, getDragTransparency, setDragTransparency, toImage, dragInitiated, drawDraggedImage, draggingOver, dragEnter, dragExit, drop, addPullToRefresh, setPullToRefresh, respondsToPointerEvents, isStickyDrag, pointerPressed, isDragAndDropOperation, pointerReleased, setVerticalScrollBounds, setHorizontalScrollBounds, isVScrollThumbGrabbed, isHScrollThumbGrabbed, isVScrollThumbHover, isHScrollThumbHover, isTensileDragEnabled, setTensileDragEnabled, getTextSelectionSupport, addDropListener, removeDropListener, addDragOverListener, removeDragOverListener, isNativeDragSource, setNativeDragSource, getNativeDragOperation, setNativeDragOperation, createNativeDragOperation, isNativeDropTarget, setNativeDropTarget, getAcceptedDropMimeTypes, setAcceptedDropMimeTypes, getAcceptedDropActions, setAcceptedDropActions, canAcceptNativeDrop, nativeDragEnter, nativeDragOver, nativeDragExit, nativeDrop, addNativeDropListener, removeNativeDropListener, addNativeDragOverListener, removeNativeDragOverListener, dragFinished, addDragFinishedListener, addStateChangeListener, removeStateChangeListener, addPointerPressedListener, addLongPressListener, addContextMenuListener, removeContextMenuListener, addMouseWheelListener, removeMouseWheelListener, addStylusListener, removeStylusListener, mouseWheel, paintRippleOverlay, removePointerPressedListener, removeLongPressListener, removeDragFinishedListener, addPointerReleasedListener, removePointerReleasedListener, addPointerDraggedListener, removePointerDraggedListener, getDragSpeed, getStyle, getPressedStyle, setPressedStyle, initUnselectedStyle, initPressedStyle, initDisabledStyle, initSelectedStyle, getUnselectedStyle, setUnselectedStyle, getSelectedStyle, setSelectedStyle, getDisabledStyle, setDisabledStyle, installDefaultPainter, requestFocus, toString, refreshTheme, refreshTheme, isDragActivated, scrollRectToVisible, scrollRectToVisible, paintBorder, paintBorderBackground, isCellRenderer, setCellRenderer, isScrollVisible, setScrollVisible, setIsScrollVisible, startEditingAsync, stopEditing, isEditing, isEditable, deinitialize, initComponent, isInitialized, setInitialized, styleChanged, getNextFocusDown, setNextFocusDown, getNextFocusUp, setNextFocusUp, getNextFocusLeft, setNextFocusLeft, getNextFocusRight, setNextFocusRight, isEnabled, setEnabled, getName, setName, initCustomStyle, deinitializeCustomStyle, isRTL, setRTL, isTactileTouch, setTactileTouch, getPropertyNames, getPropertyTypes, getPropertyTypeNames, getPropertyValue, setPropertyValue, paintLockRelease, paintLock, isSnapToGrid, setSnapToGrid, shouldBlockSideSwipe, shouldBlockSideSwipeLeft, shouldBlockSideSwipeRight, blocksSideSwipe, isFlatten, setFlatten, getTensileLength, setTensileLength, isGrabsPointerEvents, setGrabsPointerEvents, getScrollOpacityChangeSpeed, setScrollOpacityChangeSpeed, growShrink, isAlwaysTensile, setAlwaysTensile, isDraggable, setDraggable, isDropTarget, setDropTarget, isChildOf, isHideInPortrait, setHideInPortrait, cancelRepaints, getBindablePropertyNames, getBindablePropertyTypes, bindProperty, unbindProperty, getBoundPropertyValue, setBoundPropertyValue, getCloudBoundProperty, setCloudBoundProperty, getCloudDestinationProperty, setCloudDestinationProperty, getComponentState, setComponentState, setHidden, isHidden, setHidden, isHidden, announceForAccessibility, getAccessibilityText, setAccessibilityText, getSemantics, getAccessibilityNode, accessibilityChanged, accessibilityChanged, getTooltip, setTooltip
Field details
POPUP_PLACEMENT_AUTO
public static final int POPUP_PLACEMENT_AUTO = 0POPUP_PLACEMENT_ABOVE
public static final int POPUP_PLACEMENT_ABOVE = 1POPUP_PLACEMENT_BELOW
public static final int POPUP_PLACEMENT_BELOW = 2POPUP_PLACEMENT_TOP_OF_FORM
public static final int POPUP_PLACEMENT_TOP_OF_FORM = 3POPUP_PLACEMENT_BOTTOM_OF_FORM
public static final int POPUP_PLACEMENT_BOTTOM_OF_FORM = 4Constructor details
ComboBox
public ComboBox(Vector<T> items)Parameters
itemsVector<T>- set of items placed into the combo box model
ComboBox
public ComboBox(Object... items)Parameters
itemsObject...- set of items placed into the combo box model
ComboBox
public ComboBox()ComboBox
public ComboBox(ListModel<T> model)Parameters
modelListModel<T>- the model for the combo box elements and selection
Method details
isDefaultActAsSpinnerDialog
public static boolean isDefaultActAsSpinnerDialog()Returns
setDefaultActAsSpinnerDialog
public static void setDefaultActAsSpinnerDialog(boolean aDefaultActAsSpinnerDialog)Parameters
aDefaultActAsSpinnerDialogboolean- the defaultActAsSpinnerDialog to set
isDefaultIncludeSelectCancel
public static boolean isDefaultIncludeSelectCancel()Returns
setDefaultIncludeSelectCancel
public static void setDefaultIncludeSelectCancel(boolean aDefaultIncludeSelectCancel)Parameters
aDefaultIncludeSelectCancelboolean- the new value
getComboBoxImage
public Image getComboBoxImage()Returns
setComboBoxImage
public void setComboBoxImage(Image comboBoxImage)Parameters
comboBoxImageImage- the drop down icon
setUIID
public void setUIID(String uiid)Parameters
uiidString- UIID unique identifier for component type
getBaseline
public int getBaseline(int width, int height)Parameters
widthint- the component width
heightint- the component height
Returns
laidOut
protected void laidOut()getSelectedRect
public Rectangle getSelectedRect()Returns
getVisibleBounds
protected Rectangle getVisibleBounds()Returns
setSelectedIndex
public void setSelectedIndex(int selection)Parameters
selectionint- the current selected offset in the list
setSelectedIndex
public void setSelectedIndex(int selection, boolean scroll)Parameters
selectionint- the current selected offset in the list
scrollboolean- indicates whether scrolling to selection should occur if the selection is outside of view
pointerHover
public void pointerHover(int[] x, int[] y)Parameters
xint[]- the pointer x coordinate
yint[]- the pointer y coordinate
pointerHoverReleased
public void pointerHoverReleased(int[] x, int[] y)Parameters
xint[]- the pointer x coordinate
yint[]- the pointer y coordinate
createPopupDialog
protected Dialog createPopupDialog(List<T> l)Parameters
lList<T>- the list of the popup
Returns
isShowingPopupDialog
public boolean isShowingPopupDialog()showPopupDialog
protected Command showPopupDialog(Dialog popupDialog, List l)Parameters
popupDialogDialog- the popup dialog
lList- the list within
Returns
fireClicked
protected void fireClicked()createPopupList
protected List<T> createPopupList()Returns
keyReleased
public void keyReleased(int keyCode)Parameters
keyCodeint- the key code value to indicate a physical key.
pointerPressed
public void pointerPressed(int x, int y)Parameters
xint- the pointer x coordinate
yint- the pointer y coordinate
pointerDragged
public void pointerDragged(int x, int y)Parameters
xint- the pointer x coordinate
yint- the pointer y coordinate
pointerReleased
public void pointerReleased(int x, int y)Parameters
xint- the pointer x coordinate
yint- the pointer y coordinate
paint
public void paint(Graphics g)Parameters
gGraphics- the component graphics
calcPreferredSize
protected Dimension calcPreferredSize()Returns
getOrientation
public int getOrientation()Returns
isIncludeSelectCancel
public boolean isIncludeSelectCancel()Returns
setIncludeSelectCancel
public void setIncludeSelectCancel(boolean includeSelectCancel)Parameters
includeSelectCancelboolean- the new value
isActAsSpinnerDialog
public boolean isActAsSpinnerDialog()Returns
setActAsSpinnerDialog
public void setActAsSpinnerDialog(boolean actAsSpinnerDialog)Parameters
actAsSpinnerDialogboolean- the actAsSpinnerDialog to set
getPopupPlacement
public int getPopupPlacement()showPopupDialog
anchors the list when the combo is tapped.setPopupPlacement
public void setPopupPlacement(int popupPlacement)Parameters
popupPlacementint- one of
POPUP_PLACEMENT_AUTO(default),POPUP_PLACEMENT_ABOVE,POPUP_PLACEMENT_BELOW,POPUP_PLACEMENT_TOP_OF_FORM,POPUP_PLACEMENT_BOTTOM_OF_FORM