Package totalcross.ui

Class ComboBox

  • All Implemented Interfaces:
    MaterialEffect.SideEffect, HasValue<java.lang.Object>
    Direct Known Subclasses:
    ComboBoxEditable

    public class ComboBox
    extends Container
    implements MaterialEffect.SideEffect, HasValue<java.lang.Object>
    ComboBox is an implementation of a ComboBox, with the drop down window implemented by the ComboBoxDropDown class.

    Note: the color used in the setBackground method will be used in the button only. The background color of the control will be a lighter version of the given color.

    • Field Detail

      • arrowStyle

        public static int arrowStyle
      • fullHeight

        public boolean fullHeight
        If set to true, the popup window will have the height of the screen
      • fullWidth

        public boolean fullWidth
        If set to true, the popup window will have the width of the screen
      • disabledColor

        public int disabledColor
        The disable color used to fill the background and text color when disabled.
        Since:
        TotalCross 5.0
      • iconGap

        public int iconGap
        The gap between the icon and the text.
      • paddingLeft

        public int paddingLeft
        The padding between the left border and the text (or icon, if it's available).
        Since:
        TotalCross 5.0
      • paddingRight

        public int paddingRight
        The padding between the right border and the arrow.
        Since:
        TotalCross 5.0
      • paddingTop

        public int paddingTop
        The top border padding.
        Since:
        TotalCross 5.0
      • paddingBottom

        public int paddingBottom
        The bottom border padding.
        Since:
        TotalCross 5.0
      • captionPress

        public CaptionPress captionPress
        Handler for the CustomPress
      • defaultClearValueInt

        public static int defaultClearValueInt
        The default value that is set to the clearValueInt of all ComboBox. Usually this value is 0, but sometimes you may wish set it to -1, to unselect the ComboBox when clear is called.
      • checkColor

        public int checkColor
        The check color used to fill the radio button used in Android. Defaults to the #757575 color while using UI Material.
        Since:
        TotalCross 1.3
      • popupTitle

        public java.lang.String popupTitle
        The title of the PopupMenu when in Android user interface style.
        Since:
        TotalCross 1.3
      • enableSearch

        public boolean enableSearch
        Parameter passed to PopupMenu. Defaults to true.
        See Also:
        PopupMenu.enableSearch
      • caption

        public java.lang.String caption
        The caption to draw when this ComboBox is empty. In Material UI, you must set the caption BEFORE you add this control to the container; also remember to set the height to at least PREFERRED, it is 75% taller.
        See Also:
        captionColor
      • captionColor

        public int captionColor
        The caption's color.
      • captionIcon

        public Image captionIcon
        An optional caption's icon
      • usePopupMenu

        public static boolean usePopupMenu
        Set to false to don't use the PopupMenu when the user interface style is Android. This affects all ComboBoxes. If you want to change a particular ComboBox to use the standard popup list, but keep others with the PopupMenu, you can do something like:
          // at the begining of your program:
          ComboBox.usePopupMenu = true;
          // when you want to create the standalone ComboBox
          ComboBox.usePopupMenu = false; // turn flag off
          .. create the ComboBox
          ComboBox.usePopupMenu = true; // turn flag on again
         
        An internal copy of the flag is set at the constructor.
        Since:
        TotalCross 1.5
    • Constructor Detail

      • ComboBox

        public ComboBox()
        Creates an empty ComboBox
      • ComboBox

        public ComboBox​(java.lang.Object[] items)
        Creates a ComboBox with the given items
      • ComboBox

        public ComboBox​(ListBox userListBox)
        Creates a ComboBox with a PopList containing the given ListBox. You can extend the ListBox to draw the items by yourself and use this constructor so the PopList will use your class and not the default ListBox one. This constructor forces the ListBox.simpleBorder to true. Note: the listbox items must be already set.
      • ComboBox

        public ComboBox​(ComboBoxDropDown userPopList)
        Constructs a ComboBox with the given PopList.
    • Method Detail

      • add

        public void add​(java.lang.Object[] items)
        Adds an array of Objects to the Listbox
      • add

        @Deprecated
        public void add​(java.lang.Object[] items,
                        int startAt,
                        int size)
        Deprecated.
        Use add(Object[]) instead.
        Adds an array of Objects to the Listbox
      • add

        public void add​(java.lang.Object item)
        Adds an Object to the Listbox. This method is very slow if used in loop; use the add(Object[]) to add a bunch of objects instead.
      • addWrapping

        public int addWrapping​(java.lang.String text)
        Adds the given text to this ListBox, breaking the text if it goes beyond the ListBox' limits, and also breaking if it contains \n. Returns the number of lines. Note that each part of the text is considered a new item. This method is slower than the other add methods.
        Since:
        TotalCross 1.24
      • insert

        public void insert​(java.lang.Object item,
                           int index)
        Adds an Object to the Listbox at the given index
      • removeAll

        public void removeAll()
        Empties the ListBox
        Overrides:
        removeAll in class Container
      • remove

        public void remove​(java.lang.Object item)
        Removes an Object from the Listbox
      • remove

        public void remove​(int itemIndex)
        Removes an Object from the Listbox at the given index.
      • setItemAt

        public void setItemAt​(int i,
                              java.lang.Object s)
        Sets the Object at the given Index, starting from 0
      • getItemAt

        public java.lang.Object getItemAt​(int i)
        Get the Object at the given Index
      • getSelectedItem

        public java.lang.Object getSelectedItem()
        Returns the selected item of the ListBox
      • getSelectedIndex

        public int getSelectedIndex()
        Returns the position of the selected item of the ListBox
      • getItems

        public java.lang.Object[] getItems()
        Returns all items in this ComboBox
      • indexOf

        public int indexOf​(java.lang.Object name)
        Returns the index of the item specified by the name
      • setCursorColor

        public void setCursorColor​(int color)
        Sets the cursor color for this ComboBox. The default is equal to the background slightly darker.
      • setSelectedItem

        public void setSelectedItem​(java.lang.Object name)
        Selects an item. If the name is not found, the currently selected item is not changed.
        Since:
        SuperWaba 4.01
      • setSelectedItem

        public void setSelectedItem​(java.lang.Object name,
                                    boolean sendPress)
        Selects an item. If the name is not found, the currently selected item is not changed.
        Since:
        SuperWaba 4.01
      • setSelectedIndex

        public void setSelectedIndex​(int i)
        Select the given index. Choose "-1" if you want to blank the ComboBox view box.
      • setSelectedIndex

        public void setSelectedIndex​(int i,
                                     boolean sendPressEvent)
        Select the given index, and optionally sends a PRESSED event. Choose "-1" if you want to blank the ComboBox view box.
      • size

        public int size()
        Returns the number of items
      • getPreferredWidth

        public int getPreferredWidth()
        Description copied from class: Control
        Returns the preferred width of this control.
        Overrides:
        getPreferredWidth in class Control
      • setNinePatch

        public void setNinePatch​(Image img,
                                 int corner,
                                 int side)
        Description copied from class: Control
        Set the NinePatch of the control. This is used to draw the background image.
        Overrides:
        setNinePatch in class Control
        Parameters:
        img - The image to make the NinePatch. The image must be on a NinePatch format.
        corner - The size, in pixels, of the NinePatch's corner
        side - The size, in pixels, of the NinePatch's side.
      • getPreferredHeight

        public int getPreferredHeight()
        Description copied from class: Control
        Returns the preferred height of this control.
        Overrides:
        getPreferredHeight in class Control
      • onFontChanged

        protected void onFontChanged()
        Passes the font to the pop list
        Overrides:
        onFontChanged in class Control
      • setBackForeItemColors

        public void setBackForeItemColors​(IntHashtable ihtFore,
                                          IntHashtable ihtBack)
        Sets the ihtForeColors and ihtBackColors for the ListBox used with this ComboBox. Note that null is a valid value, used when you always want to use the default color.
        Since:
        TotalCross 1.0 beta 4
      • onBoundsChanged

        protected void onBoundsChanged​(boolean screenChanged)
        Description copied from class: Control
        Called after a setRect.
        Overrides:
        onBoundsChanged in class Control
        Parameters:
        screenChanged - If the bounds were changed due to a screen change (rotation, collapse)
      • onEvent

        public void onEvent​(Event event)
        Description copied from class: Control
        Called to process key, pen, control and other posted events.
        Overrides:
        onEvent in class Control
        Parameters:
        event - the event to process
        See Also:
        Event, KeyEvent, PenEvent
      • updatePopRect

        protected void updatePopRect()
      • popup

        public void popup()
        Pops up the ComboBoxDropDown
      • unpop

        public void unpop()
        Unpops the ComboBoxDropDown.
        Since:
        TotalCross 1.2
      • onColorsChanged

        protected void onColorsChanged​(boolean colorsChanged)
        Description copied from class: Control
        Called after a setEnabled, setForeColor and setBackColor and when a control has been added to a Container. If colorsChanged is true, it was called from setForeColor/setBackColor/Container.add; otherwise, it was called from setEnabled
        Overrides:
        onColorsChanged in class Container
      • getArrowColor

        protected int getArrowColor()
        Used to determine the arrow color in Material UI when its drawing.
      • onPaint

        public void onPaint​(Graphics g)
        paint the combo's border and the current selected item
        Overrides:
        onPaint in class Container
        Parameters:
        g - the graphics object for drawing
        See Also:
        Graphics
      • drawSelectedItem

        protected void drawSelectedItem​(Graphics g)
      • qsort

        public void qsort()
        Sorts the items of this combobox, and then unselects the current item.
      • qsort

        public void qsort​(boolean caseless)
        Sorts the elements of this ListBox. The current selection is cleared.
        Parameters:
        caseless - Pass true to make a caseless sort, if the items are Strings.
      • enableHorizontalScroll

        public void enableHorizontalScroll()
        Adds support for horizontal scroll on this listbox. Two buttons will appear below the vertical scrollbar. The add, replace and remove operations will be a bit slower because the string's width will have to be computed in order to correctly set the maximum horizontal scroll.
        Since:
        SuperWaba 5.6
      • selectLast

        public void selectLast()
        Selects the last item added to this combobox, doing a scroll if needed. Calls repaintNow.
        Since:
        SuperWaba 5.6
      • clear

        public void clear()
        Clears this control, selecting index clearValueInt (0 by default); uses clearValueStr if set, instead.
        Overrides:
        clear in class Container
      • getFocusableControls

        public void getFocusableControls​(Vector v)
        Description copied from class: Container
        Get a list of child controls of this container which are focus candidates
        Overrides:
        getFocusableControls in class Container
        Parameters:
        v - A vector into which to add the focus candidates.
      • handleGeographicalFocusChangeKeys

        public Control handleGeographicalFocusChangeKeys​(KeyEvent ke)
        Description copied from class: Control
        Used by the main event loop to give the currently focused control an opportunity to act directly on the KeyEvent.
        Overrides:
        handleGeographicalFocusChangeKeys in class Control
        Parameters:
        ke - The KeyEvent to be processed
        Returns:
        The control that should get focus as a result of this KeyEvent. Null if this control did not handle the KeyEvent.
        See Also:
        Settings.geographicalFocus
      • getListBox

        public ListBox getListBox()
        Returns the ListBox used when this combobox is opened.
      • setSelectedItemStartingWith

        public boolean setSelectedItemStartingWith​(java.lang.String text,
                                                   boolean caseInsensitive)
        Selects the item that starts with the given text
        Parameters:
        text - The text string to search for
        caseInsensitive - If true, the text and all searched strings are first converted to lowercase.
        Returns:
        If an item was found and selected.
        Since:
        TotalCross 1.13
      • setSelectedItemStartingWith

        public boolean setSelectedItemStartingWith​(java.lang.String text,
                                                   boolean caseInsensitive,
                                                   boolean sendPress)
        Selects the item that starts with the given text
        Parameters:
        text - The text string to search for
        caseInsensitive - If true, the text and all searched strings are first converted to lowercase.
        sendPress - If true, sends the PRESSED event
        Returns:
        If an item was found and selected.
        Since:
        TotalCross 1.13
      • setPop

        public void setPop​(ComboBoxDropDown lb)
        Replaces the original ComboBoxDropDown by the given one.
        Since:
        TotalCross 1.5
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • getValue

        public java.lang.Object getValue()
        Specified by:
        getValue in interface HasValue<java.lang.Object>
      • setValue

        public void setValue​(java.lang.Object value)
        Specified by:
        setValue in interface HasValue<java.lang.Object>