ComboBox Demo and Documentation

Display sketch code: ComboBoxDemo.pde. The code for ComboBox is in Clickable.pde

The ComboBox can be at standard combobox as shown on the left or as a non-traditional combobox as done on the right.

There is a third constructor but it is designed to be used by Menu and cannot be used directly.

Fields x, y, w, and h are for location of the top left corner, width, and height of the permanent button. h has a default value.

class ComboBox
extends Clickable

A ComboBox consists of a "permanently" displayed button and a dropdown ListBox that is hidden except for when the permanent button is clicked. Except when extended by Menu, the permanent button includes a down arrow.

Additional fields

final int WIDTH_DOWN_ARROW = 20
final int ITEM_HEIGHT = 20
Button[] comboListArray - The list of buttons in the list box.
Button comboButton - Part of the permanent button.
Button comboArrow - Part of the permanent button but not used by the Menu extension.
ListBox comboListBox - The drop down ListBox.
color[] c, overC - The normal color and the colors used when the mouse is over an item.
boolean listBoxVisible - If true, its drop down ListBox is visible.
int pnlHeight - Used only in second constructor as the height of a panel to hold the buttons.
boolean showSelectedLabel - The permanent combo box show the label of the selected item.
** ComboBox(int leftX, int topY, int theWidth, String[] labels,
                   - color stdColor, color overColor,
                   - color selected, color overSelected)
- It makes a traditional looking combo box automatically generating a button for each label in the labels array. If you don't want to show a button as being selected, use the same colors both standard and selected.
** ComboBox(int leftX, int topY, int theWidth, int panelHeight,
                    ButtonGroup aBtnGrp,
                    color stdColor, color overColor,
                    color selected, color overSelected)
- It creates a possibly nontraditional looking combo box given a ButtonGroup. If panelHeight > 0, a panel is created to hold the buttons. Important note: The location given in the buttons in the ButtonGroup is relative to the location of the menu button in the permanent button. Normally all the buttons should be given x location 0. The y location of the first button should be 0. The y location of additional buttons should be the y location of the previous button plus the height of the previous button.
# ComboBox(String label)
- This constructor does almost nothing and is designed to be used by Menu and possibly other extensions that will take care of the construction by themselves. Typically comboArrow should be set to null so the ComboBox methods will not draw it.
** void setEnabled(boolean b)
- Sets the ComboBox and all its parts to enabled or disabled.
** void setVisible(boolean b)
- Sets the ComboBox and all its parts to visible or hidden.
** void setLabel(String theLabel)
- Sets the label of the menu item which is displayed in the button in the menu bar.
** void setPermanentLabel(String theLabel)
- Sets a permanent label for the menu items or combo boxes whose label should not change to match the selection. It sets the label. Can be used for nontraditional ComboBoxes.
* void display()
- Displays all the buttons in the ComboBox assuming it is visible. The permanent button is always displayed. The dropdown ListBox is displayed, if needed. When the list is dropped down, the panel is displayed behind the buttons if it has been given a positive height in the second constructor. The label of the permanent button is the label from the selected button unless setPermanentLabel(...) has been used. (This is automatically done by menus.)
* boolean isOver()
- Returns true if the mouse cursor is over any of the part of the visible ComboBox.
# void hideSomething()
- Hides the ListBox unless the mouse is over the ComboBox. This needs to be called by mouseReleased() whenever the ComboBox exists. This is done automatically if the ComboBox is part of a ClickableGroup.
* boolean checkForClick()
- It first checks the comboButton and the comboArrow (if not null) to see if either were clicked. If so, the comboListBox is made visible or hidden as needed, it marks the comboButton as clicked, and assigns clickedItemNum the value COMBO_BUTTON. If the comboButton wasn't clicked and comboListBox is visible, it checks it to see if it was clicked. If so, it sets clickedItemNum, selectedItemNum, assigns comboButton's label to the label of the comboListBox and the comboListBox is hidden. In either case, "clicked" is set true and true is returned. If neither part of the ComboBox was clicked or if stillSearching_is false, the clicked field is false and false is returned.
* boolean hasBeenClicked()
- Returns true if either the comboButton (or equivalently the comboArrow) or the comboListBox has been clicked. Clears the clicked field.
** int getClickedItemNum()
- Returns clickedItemNum.
** void setSelectedItemNum(int b)
- Sets the value SelectedItemNum and indirectly selects the appropriate button. Also indirectly updates the label for ComboBoxes (but not for menus or if setPermanentLabel(...) has been used).
** int getSelectedItemNum()
- Returns selectedItemNum.
** Button getSelectedItem()
- Returns the selected (clicked) item.
** String getSelectedLabel()
- Returns the label of the selected item.
** Button getItem(getItem)
- Returns the specified item - either the comboButton (i = COMBO_BUTTON) or a member of comboListBox.
** int getGroupLength()
- Returns the number of buttons in the drop down list box.
** void setScrollable(boolean canScroll)
- Used to specify that this item and all its components is or is not scrollable.
String toString()
- Returns a string with lots of info about the Menu.

Method use codes:

** This constructor or method is designed to be called in the user sketch.

* This method is normally not called in the user's sketch unless the Clickable item was not been included in a ClickableGroup. ClickableGroup automatically uses the method as needed. Exception: This method may be needed if the user is extending Clickable with a new class.

# This constructor or method is rarely used in the user program. In Java, it would probably marked "private". Clickable, ScrollBar, and some other abstract constructors would never be called directly by the user sketch because they do nothing useful unless the user is extending a class.


Inherited from Clickable

Fields
NOTHING_CLICKED, COMBO_BUTTON, DISABLED_COLOR, borderColor, c, clicked, clickedItemNum, colorState, enabled, h, label, over, overC, scrollable, selected, selectedItemNum, visible, w, x, y

Constructors
Clickable

Methods
checkForClick, display, doNotSelect, getClickedItemNum, getColorState, getH, getItem, getLabel, getSelected, getSelectedItem, getSelectedItemLabel, getSelectedItemNum, getW, getX, getY, hasBeenClicked, hideSomething, isOver, setBorderColor, setColorState, setH, setLabel, setScrollable, setSelected, setSelectedItemNum, setEnabled, setVisible, setW, setX, setY, sX, sY

Return to the top of the page

Revised: 04/11/2014