public class ListBox extends Container implements Scrollable
Note: the color used in the setBackColor()
method will be used
in the scrollbar only. The background color of the control will be a lighter
version of the given color.
Here is an example showing how it can be used:
import totalcross.ui.*; public class MyProgram extends MainWindow { ListBox lb; public void initUI() { lb = new ListBox(); add(lb); lb.add(new String[]{"Daniel","Jenny","Helge","Sandra"}); lb.add("Marc"); // you may set the rect by using PREFERRED only after the items were added. lb.setRect(LEFT,TOP,PREFERRED,PREFERRED); // use control's preferred width based on the size of the elements } public void onEvent(Event event) { switch (event.type) { case ControlEvent.PRESSED: if (event.target == lb) Object element = lb.getSelectedItem(); // in most cases, this is just a String and may be casted to such } } }The first item has index 0.
Modifier and Type | Class and Description |
---|---|
static interface |
ListBox.CustomDrawingItem
An interface that makes easier to draw custom items.
|
static class |
ListBox.IconItem
Used to show an icon or two and a text.
|
Control.TranslucentShape
Modifier and Type | Field and Description |
---|---|
boolean |
autoResizeIcon
Resize the icon to the iconSize automatically
|
protected int |
back0 |
protected int |
back1 |
protected int |
btnX |
protected int |
btnX0 |
protected int |
customCursorColor |
protected int |
dataCol
Used by the DBListBox to store the data column that is displayed.
|
static double |
DEFAULT_ITEM_HEIGHT_FACTOR |
boolean |
drawBorder
Deprecated.
|
static int |
EXTRA_HEIGHT_FACTOR
When the ListBox has horizontal buttons and its height divided by the button
height is greater than this value (10), the horizontal button heights are
increased.
|
int |
extraHorizScrollButtonHeight
The extra height of the horizontal scroll buttons.
|
protected Flick |
flick
The Flick object listens and performs flick animations on PenUp events when
appropriate.
|
int |
iconGap
The gap between the icon and the text.
|
int |
iconSize
The size used when the icon is automatically resized
|
protected double |
ihFactor |
IntHashtable |
ihtBackColors
IntHashtable used to specify different background colors for some items.
|
IntHashtable |
ihtForeColors
IntHashtable used to specify different foreground colors for some items.
|
protected int |
itemCount |
static double |
itemHeightFactor
In finger touch devices, sets a factor by which the font height will be
multiplied to increase the item's height.
|
protected Vector |
items |
protected int |
offset |
int |
paddingBottom |
int |
paddingLeft |
int |
paddingRight |
int |
paddingTop |
protected ScrollBar |
sbar |
protected int |
selectedIndex |
protected boolean |
simpleBorder |
protected int |
tempSelectedIndex |
static boolean |
useFullWidthOnSelection
If true, all ListBox will have the selection bar drawn in the full width
instead of the selected's text width
|
protected int |
visibleItems |
int |
visibleLines
Sets the number of visible lines, used to make PREFERRED height return the
given number of lines as the grid height.
|
protected int |
xOffset |
alwaysEraseBackground, BACKGROUND_CYLINDRIC_SHADED, BACKGROUND_SHADED, BACKGROUND_SHADED_INV, BACKGROUND_SOLID, backgroundStyle, BORDER_LOWERED, BORDER_NONE, BORDER_RAISED, BORDER_ROUNDED, BORDER_SIMPLE, BORDER_TOP, borderColor, borderRadius, children, controlFound, finishedStart, ignoreOnAddAgain, ignoreOnRemove, insets, lastH, lastScreenWidth, lastW, lastX, lastY, numChildren, started, tabOrder, tail, TRANSITION_TIME
AFTER, alphaValue, appId, appObj, asContainer, asWindow, backColor, BEFORE, BOTTOM, BOTTOM_OF, BRIGHTER_BACKGROUND, callListenersOnAllTargets, CENTER, CENTER_OF, clearValueInt, clearValueStr, DARKER_BACKGROUND, DP, effect, enableUpdateScreen, esce, eventsEnabled, FILL, fillColor, FIT, floating, fm, fmH, focusHandler, focusLess, focusOnPenDown, focusTraversable, font, FONTSIZE, foreColor, height, ignoreInsets, isHighlighting, isTablet, KEEP, keepDisabled, keepEnabled, LEFT, next, nextTabControl, npParts, offscreen, offscreen0, onEventFirst, parent, PARENTSIZE, PARENTSIZEMAX, PARENTSIZEMIN, PREFERRED, prev, RANGE, repositionAllowed, RIGHT, RIGHT_OF, SAME, SCREENSIZE, SCREENSIZEMAX, SCREENSIZEMIN, setFont, setH, setRel, setW, setX, SETX_NOT_SET, setY, tempW, textShadowColor, TOP, translucentShape, transparentBackground, uiAdjustmentsBasedOnFontHeightIsSupported, uiAndroid, UICONST, uiFlat, uiHolo, uiMaterial, uiVista, visible, width, WILL_RESIZE, x, y
Constructor and Description |
---|
ListBox()
Creates an empty Listbox.
|
ListBox(java.lang.Object[] items)
Creates a Listbox with the given items.
|
Modifier and Type | Method and Description |
---|---|
void |
add(Control control)
Do nothing.
|
void |
add(java.lang.Object item)
Adds an Object to the Listbox
|
void |
add(java.lang.Object[] moreItems)
Adds an array of Objects to the Listbox
|
void |
add(java.lang.Object[] moreItems,
int startAt,
int size)
Deprecated.
Use
add(Object[]) instead |
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.
|
boolean |
canScrollContent(int direction,
java.lang.Object target)
Checks if there is room to flick this container in the given direction.
|
void |
clear()
Clears this control, selecting index clearValueInt.
|
protected void |
drawCursor(Graphics g,
int sel,
boolean on)
This method is used to draw the cursor around the desired item
|
protected void |
drawItem(Graphics g,
int index,
int dx,
int dy)
You can extend ListBox and overide this method to draw the items
|
protected void |
drawItems(Graphics g,
int dx,
int dy,
int greatestVisibleItemIndex) |
protected void |
drawSelectedItem(Graphics g,
int from,
int to) |
protected void |
drawSelectedItem(Graphics g,
int index,
int dx,
int dy,
int w)
You can extend ListBox and overide this method to draw the items
|
void |
enableHorizontalScroll()
Adds support for horizontal scroll on this listbox.
|
protected void |
endSelection() |
protected void |
find(char c)
Searches this ListBox for an item with the first letter matching the given
char.
|
void |
flickEnded(boolean atPenDown)
Called when the flick animation is ended.
|
boolean |
flickStarted()
Called when the flick animation is started.
|
protected int |
getCursorColor(int index) |
int |
getEffectH() |
int |
getEffectW() |
int |
getEffectX() |
int |
getEffectY() |
Flick |
getFlick()
Returns the current flick object.
|
void |
getFocusableControls(Vector v)
Get a list of child controls of this container which are focus candidates
|
java.lang.Object |
getItemAt(int i)
Get the Object at the given Index.
|
protected int |
getItemHeight(int i) |
java.lang.Object[] |
getItems()
Returns all items in this ListBox.
|
protected java.lang.Object[] |
getItemsArray()
Used internally
|
protected int |
getItemWidth(int index)
Returns the width of the given item index with the current fontmetrics.
|
int |
getPreferredHeight()
Returns the number of items multiplied by the font metrics height
|
int |
getPreferredWidth()
Returns the preferred width, ie, the size of the largest item plus the size
of the scrollbar.
|
int |
getScrollPosition(int direction)
Returns the current position given the direction.
|
int |
getSelectedIndex()
Returns the position of the selected item of the Listbox or -1 if the listbox
has no selected index yet.
|
java.lang.Object |
getSelectedItem()
Returns the selected item of the Listbox or an empty String Object if none is
selected
|
java.lang.String |
getText()
Returns the string of the selected item or "" if none is selected.
|
Control |
handleGeographicalFocusChangeKeys(KeyEvent ke)
Used by the main event loop to give the currently focused control an opportunity to act directly on
the KeyEvent.
|
protected void |
handleSelection(int newSelection) |
protected boolean |
hasIconItemLeft() |
protected boolean |
hasIconItemRight() |
boolean |
hideScrollBarIfNotNeeded()
This method hides the scrollbar if its not needed, i.e., if horizontal scroll
is disabled and the preferred height is smaller than the actual height.
|
int |
indexOf(java.lang.Object name)
Returns the index of the item specified by the name, or -1 if not found.
|
void |
insert(java.lang.Object item,
int index)
Adds an Object to the Listbox at the given index
|
protected void |
leftReached() |
protected void |
onBoundsChanged(boolean screenChanged)
This is needed to recalculate the box size for the selected item if the
control is resized by the main application
|
protected void |
onColorsChanged(boolean colorsChanged)
Called after a setEnabled, setForeColor and setBackColor and when a control has
been added to a Container.
|
void |
onEvent(Event event)
Handles the events for this control.
|
void |
onPaint(Graphics g)
Draws the border (if any).
|
void |
qsort()
Sorts the elements of this ListBox.
|
void |
qsort(boolean caseless)
Sorts the elements of this ListBox.
|
void |
remove(Control control)
Do nothing.
|
void |
remove(int itemIndex)
Removes the Object at the given index from the Listbox
|
void |
remove(java.lang.Object item)
Removes an Object from the Listbox
|
void |
removeAll()
Empties this ListBox, setting all elements of the array to
null
so they can be garbage collected. |
boolean |
scrollContent(int xDelta,
int yDelta,
boolean fromFlick)
Performs a relative move.
|
void |
selectLast()
Selects the last item added to this listbox, doing a scroll if needed.
|
void |
selectLast(boolean sendPressEvent)
Selects the last item added to this listbox, doing a scroll if needed, and
sending or not the event.
|
void |
setCursorColor(int color)
Sets the cursor color for this ListBox.
|
void |
setEnabled(boolean enabled)
Sets if this container and all childrens can or not accept events
|
void |
setItemAt(int i,
java.lang.Object s)
Replace the Object at the given index, starting from 0
|
void |
setSelectedIndex(int i)
Select the given index and scroll to it if necessary.
|
void |
setSelectedIndex(int i,
boolean sendPressEvent)
Select the given index and scroll to it if necessary, sending or not the
pressed event.
|
void |
setSelectedItem(java.lang.Object name)
Selects the given name.
|
boolean |
setSelectedItemStartingWith(java.lang.String text,
boolean caseInsensitive)
Selects the item that starts with the given text
|
void |
setSimpleBorder(boolean simpleBorder)
Sets the border of the listbox to be not 3d if flag is true.
|
protected void |
setTextAreaClip(Graphics g,
int dx,
int dy) |
int |
size()
Returns the number of items
|
boolean |
wasScrolled()
Returns true if the control was scrolled since last pen down
|
add, add, add, add, broadcastEvent, fillBackground, findChild, findNearestChild, findNextFocusControl, getBorderRadius, getBorderStyle, getChildren, getChildrenCount, getClientRect, getClientRect, getFirstChild, getInsets, incLastX, incLastY, initUI, isPressed, moveFocusToNextControl, moveFocusToNextEditable, onAddAgain, onRemove, onSwapFinished, paintChildren, resize, resizeHeight, resizeWidth, setBorderRadius, setBorderStyle, setFocusTraversable, setHighlighting, setInsets, setPressColor, setPressed, swapToTopmostWindow
_onEvent, addEnabledStateListener, addFocusListener, addFontChangeHandler, addGridListener, addHandler, addHighlightListener, addKeyListener, addListContainerListener, addMouseListener, addMultiTouchListener, addPenListener, addPressListener, addPushNotificationListener, addSizeChangeHandler, addTimer, addTimer, addTimerListener, addValueChangeHandler, addWindowListener, bringToFront, changeHighlighted, contains, drawTranslucentBackground, getAbsoluteRect, getBackColor, getDoEffect, getEventListeners, getFont, getForeColor, getGap, getGraphics, getHeight, getNext, getParent, getParentWindow, getPos, getPressedEvent, getPrev, getRect, getSize, getTextShadowColor, getWidth, getX, getX2, getY, getY2, hadParentScrolled, hasFocus, internalSetEnabled, intXYWH, isActionEvent, isChildOf, isDisplayed, isEnabled, isFloating, isInsideOrNear, isObscured, isTopMost, isVisible, isVisibleAndInside, onFontChanged, onWindowPaintFinished, post, postEvent, postPressedEvent, releaseScreenShot, removeEnabledStateListener, removeFocusListener, removeGridListener, removeHandler, removeHighlightListener, removeKeyListener, removeListContainerListener, removeMouseListener, removeMultiTouchListener, removePenListener, removePressListener, removePushNotificationListener, removeTimer, removeTimerListener, removeWindowListener, repaint, repaintNow, reposition, reposition, repositionChildren, requestFocus, resetSetPositions, resetStyle, safeRepaintNow, safeUpdateScreen, sendToBack, setBackColor, setBackForeColors, setDoEffect, setFloating, setFocusLess, setFont, setForeColor, setNinePatch, setNinePatch, setRect, setRect, setRect, setRect, setSet, setTextShadowColor, setTranslucent, setVisible, showTip, takeInitialScreenShot, takeScreenShot, translateFromOrigin, uiStyleChanged, updateScreen, updateTemporary, willOpenKeyboard
protected Vector items
protected int offset
protected int selectedIndex
protected int tempSelectedIndex
protected int itemCount
protected int visibleItems
protected int btnX
protected int btnX0
protected ScrollBar sbar
protected boolean simpleBorder
protected int xOffset
protected int back0
protected int back1
protected int customCursorColor
public int paddingLeft
public int paddingRight
public int paddingTop
public int paddingBottom
public boolean autoResizeIcon
public int iconSize
public int iconGap
@Deprecated public boolean drawBorder
public static int EXTRA_HEIGHT_FACTOR
public IntHashtable ihtBackColors
list.ihtBackColor = new IntHashtable(10); ihtBackColors.put(10, 0xAABBCC); // will make line number 10 with back color 0xAABBCC.Specify a null value if you want to use the default back color (this also makes drawing faster). Note that its up to you to update the hashtable if an item is inserted or removed.
public IntHashtable ihtForeColors
list.ihtForeColor = new IntHashtable(10); ihtForeColors.put(10, Color.RED); // will make line number 10 with fore color RED.Specify a null value if you want to use the default fore color (this also makes drawing faster). Note that its up to you to update the hashtable if an item is inserted or removed.
public int extraHorizScrollButtonHeight
EXTRA_HEIGHT_FACTOR
,
enableHorizontalScroll()
protected Flick flick
public int visibleLines
public static boolean useFullWidthOnSelection
public static final double DEFAULT_ITEM_HEIGHT_FACTOR
public static double itemHeightFactor
ListBox.itemHeightFactor = 1; ... create listbox ListBox.itemHeightFactor = ListBox.DEFAULT_ITEM_HEIGHT_FACTOR;
protected double ihFactor
protected int dataCol
public ListBox()
Creates an empty Listbox.
Tips:public ListBox(java.lang.Object[] items)
Creates a Listbox with the given items.
Tips:public boolean flickStarted()
Scrollable
flickStarted
in interface Scrollable
public void flickEnded(boolean atPenDown)
Scrollable
flickEnded
in interface Scrollable
atPenDown
- Flag indicating if the animation ended due to a pendown event.public boolean canScrollContent(int direction, java.lang.Object target)
Scrollable
canScrollContent
in interface Scrollable
direction
- The direction we want to flick given by one of the following constants:target
- The target of the series of PEN events that triggered this flick attempt.public boolean scrollContent(int xDelta, int yDelta, boolean fromFlick)
Scrollable
scrollContent
in interface Scrollable
xDelta
- The relative amount of pixels to move in the X axis.yDelta
- The relative amount of pixels to move in the Y axis.public int getScrollPosition(int direction)
Scrollable
getScrollPosition
in interface Scrollable
public void enableHorizontalScroll()
extraHorizScrollButtonHeight
public void add(java.lang.Object[] moreItems)
@Deprecated public void add(java.lang.Object[] moreItems, int startAt, int size)
add(Object[])
insteadpublic void add(java.lang.Object item)
public int addWrapping(java.lang.String text)
add
methods.public void insert(java.lang.Object item, int index)
public void removeAll()
null
so they can be garbage collected. Attention! If you used the same
object array to initialize two ListBoxes (or ComboBoxes), this method will
null both ListBoxes (because they use the same array reference), and you'll
get a null pointer exception!public void remove(int itemIndex)
public void remove(java.lang.Object item)
public void setItemAt(int i, java.lang.Object s)
public java.lang.Object getItemAt(int i)
public java.lang.Object getSelectedItem()
public int getSelectedIndex()
public java.lang.Object[] getItems()
protected java.lang.Object[] getItemsArray()
public int indexOf(java.lang.Object name)
public void setSelectedItem(java.lang.Object name)
public void setSelectedIndex(int i)
public void setSelectedIndex(int i, boolean sendPressEvent)
public void selectLast()
public void selectLast(boolean sendPressEvent)
public int size()
public void add(Control control)
public void remove(Control control)
public int getPreferredWidth()
getPreferredWidth
in class Control
public int getPreferredHeight()
getPreferredHeight
in class Control
protected void onBoundsChanged(boolean screenChanged)
onBoundsChanged
in class Control
screenChanged
- If the bounds were changed due to a screen change (rotation, collapse)protected void find(char c)
public void onEvent(Event event)
protected void leftReached()
protected void endSelection()
protected void handleSelection(int newSelection)
public void setEnabled(boolean enabled)
Container
setEnabled
in class Container
protected void onColorsChanged(boolean colorsChanged)
Control
onColorsChanged
in class Container
public void onPaint(Graphics g)
Container
super.onPaint(g);
, or the border will not be drawn.public int getEffectW()
getEffectW
in class Control
public int getEffectH()
getEffectH
in class Control
public int getEffectX()
getEffectX
in class Control
public int getEffectY()
getEffectY
in class Control
protected void drawItems(Graphics g, int dx, int dy, int greatestVisibleItemIndex)
protected int getItemHeight(int i)
protected void setTextAreaClip(Graphics g, int dx, int dy)
protected void drawSelectedItem(Graphics g, int from, int to)
public void setCursorColor(int color)
protected void drawItem(Graphics g, int index, int dx, int dy)
protected void drawSelectedItem(Graphics g, int index, int dx, int dy, int w)
protected int getItemWidth(int index)
protected void drawCursor(Graphics g, int sel, boolean on)
protected boolean hasIconItemLeft()
protected boolean hasIconItemRight()
protected int getCursorColor(int index)
public void setSimpleBorder(boolean simpleBorder)
public void qsort()
public void qsort(boolean caseless)
caseless
- Pass true to make a caseless sort, if the items are Strings.public void clear()
public void getFocusableControls(Vector v)
Container
getFocusableControls
in class Container
v
- A vector into which to add the focus candidates.public Control handleGeographicalFocusChangeKeys(KeyEvent ke)
Control
handleGeographicalFocusChangeKeys
in class Control
ke
- The KeyEvent to be processedSettings.geographicalFocus
public java.lang.String getText()
public boolean setSelectedItemStartingWith(java.lang.String text, boolean caseInsensitive)
text
- The text string to search forcaseInsensitive
- If true, the text and all searched strings are first
converted to lowercase.public boolean hideScrollBarIfNotNeeded()
reposition
if this method returns true. You can
call this method after all items were added.public Flick getFlick()
Scrollable
getFlick
in interface Scrollable
public boolean wasScrolled()
Scrollable
wasScrolled
in interface Scrollable