Search…
ComboBox

Overview

It is a compressed checkbox that, when clicked, expands, allowing the user to choose an item from several possible options
This sample code is only from the ComboBox, to see the complete sample, including the ListBox, go to github.

Source Code

ComboBoxSample.java
1
import totalcross.sys.Settings;
2
import totalcross.ui.ComboBox;
3
import totalcross.ui.Label;
4
import totalcross.ui.ScrollContainer;
5
import totalcross.ui.dialog.MessageBox;
6
7
public class ComboBoxSample extends ScrollContainer {
8
9
private ComboBox simpleComboBox;
10
private ComboBox popupComboBox;
11
12
private int gap = (int) (Settings.screenDensity * 20);
13
14
@Override
15
public void initUI() {
16
try {
17
setScrollBars(false, true);
18
setBackForeColors(0xF7F7F7, 0x000000);
19
20
String[] items = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" };
21
22
Label lbCombos = new Label("Combos", CENTER);
23
lbCombos.setFont(lbCombos.getFont().asBold());
24
add(lbCombos, LEFT + gap, AFTER + gap * 2, FILL - gap, PREFERRED);
25
26
ComboBox.usePopupMenu = false;
27
simpleComboBox = new ComboBox(items);
28
simpleComboBox.caption = "Numbers with Dropdown";
29
simpleComboBox.setForeColor(0x000000);
30
31
add(simpleComboBox, LEFT + gap, AFTER + gap / 2, FILL - gap, PREFERRED);
32
33
ComboBox.usePopupMenu = true;
34
popupComboBox = new ComboBox(items);
35
popupComboBox.caption = "Numbers with Popup";
36
popupComboBox.setForeColor(0x000000);
37
38
add(popupComboBox, LEFT + gap, AFTER + gap / 2, FILL - gap, PREFERRED);
39
40
} catch (Exception e) {
41
MessageBox.showException(e, true);
42
}
43
}
44
}
Copied!

Attributes

Type
Name
Description
int
checkColor
Changing the value of this variable will change the color of the RadioButton, that only appears in the Android environment, or the color of the arrow in other evironments where MaterialUI is enabled
Boolean
enableSearch
When the number of items in a ComboBox is greater than 10, an area above the popup list is intended for item searching. By default, the value of this attribute is true, set it to false if you do not want this item search area
String
popupTitle
Changes the popup list title
Boolean
usePopMenu
Assign true to this variable before instanciate a new Combobox to show items in a popup window menu.

Methods

Type
Name
Description
Constructor
ComboBox( )
Creates an empty ComboBox
Constructor
ComboBox(Object[ ] items)
Creates a ComboBox with the given items
Constructor
ComboBox(ListBox userListBox)
Creates a ComboBox with a popup list containing the given ListBox. You can create a class that extends a ListBox to draw the items by yourself and then pass it as parameter to this constructor, so the popup list will use your class instead
Constructor
ComboBox(ComboBoxDropDown userPopList)
Creates a ComboBox with the given PopList
void
add(Object item)
Adds an object to the Listbox
void
add(Object[ ] items)
Adds an array of Objects to the Listbox
void
clear( )
Resets the selected index to the value of the defaultClearValueInt attribute; the default value is 0
Object
getItemAt(int i)
Get the object at the given index
Object[ ]
getItems( )
Returns all items in this ComboBox
ListBox
getListBox( )
Returns the ListBox used to show the items of the ComboBox
Object
getSelectedItem( )
Returns the selected item of the ListBox
void
remove(int itemIndex)
Removes an object from the Listbox at the given index
void
remove(Object item)
Removes an object from the Listbox
void
removeAll( )
Empties the items of the ComboBox
void
setItemAt(int i, Object s)
Sets the object at the given index
void
setSelectedIndex(int i)
Select the given index
void
setSelectedIndex(int i, boolean sendPressEvent)
Select the given index, and optionally sends a PRESSED event
void
setSelectedItem(Object name, boolean sendPress)
Select an item, and optionally sends a PRESSED event

References

Last modified 2yr ago