waba.ui
Class ListBox

java.lang.Object
  |
  +--waba.ui.Control
        |
        +--waba.ui.Container
              |
              +--waba.ui.ListBox
Direct Known Subclasses:
ColorList

public class ListBox
extends Container

ListBox is a simple implementation of a Listbox. You can use the up/down keys to scroll and enter the first letter of an item to select it.

Note: the color used in the setBackground 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 waba.ui.*;

 public class MyProgram extends MainWindow
 {
 ListBox lb;

 public void onStart()
 {
   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.


Field Summary
protected  int btnX
           
protected  int itemCount
           
protected  Vector items
           
protected  int offset
           
protected  ScrollBar sbar
           
protected  int selectedIndex
           
protected  boolean simpleBorder
           
protected  int visibleItems
           
 
Fields inherited from class waba.ui.Container
BORDER_LOWERED, BORDER_NONE, BORDER_RAISED, BORDER_SIMPLE, children, lastH, lastW, lastX, lastY, parentWindow, tail
 
Fields inherited from class waba.ui.Control
AFTER, appId, asContainer, asWindow, backColor, backDis, BEFORE, BOTTOM, CENTER, enabled, FILL, FIT, fm, fmH, focusLess, font, foreColor, foreDis, height, LEFT, parent, PREFERRED, RANGE, RIGHT, SAME, TOP, visible, width, x, x2, y, y2
 
Constructor Summary
ListBox()
          Creates an empty Listbox.
ListBox(Object[] items)
          Creates a Listbox with the given items.
 
Method Summary
 void add(Control control)
          do nothing
 void add(Object item)
          Adds an Object to the Listbox
 void add(Object[] moreItems)
          Adds an array of Objects to the Listbox
protected  void drawCursor(Graphics g, int sel, boolean on)
           
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 find(char c)
          Searches this ListBox for an item with the first letter matching the given char.
 Object getItemAt(int i)
          Get the Object at the given Index
 Object[] getItems()
          Returns all items in this ListBox.
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 20.
 int getSelectedIndex()
          Returns the position of the selected item of the Listbox or -1 if the listbox has no selected index yet.
 Object getSelectedItem()
          Returns the selected item of the Listbox or an empty Object if none is selected
 int indexOf(Object name)
          Returns the index of the item specified by the name
 void insert(Object item, int index)
          Adds an Object to the Listbox at the given index
protected  void onBoundsChanged()
          Called after an setRect.
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)
          Called to process key, pen, control and other posted events.
 void onPaint(Graphics g)
          Draws the border (if any).
 void qsort()
          Sorts the elements of this ListBox.
 void remove(Control control)
          do nothing
 void remove(int itemIndex)
          Removes the given index from the Listbox
 void remove(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.
 void select(int i)
          Select an item and scroll to it if necessary.
 void setCursorColor(Color 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, Object s)
          Sets the Object at the given Index, starting from 0
 void setSimpleBorder(boolean simpleBorder)
          Sets the border of the listbox to be not 3d if flag is true.
 int size()
          Returns the number of items
 
Methods inherited from class waba.ui.Container
add, add, broadcastEvent, findChild, getChildren, getClientRect, onStart, paintChildren, setBorderStyle
 
Methods inherited from class waba.ui.Control
addTimer, contains, createGraphics, getAbsoluteRect, getBackColor, getFont, getFontMetrics, getForeColor, getNext, getParent, getParentWindow, getPos, getRect, getSize, isDisplayed, isEnabled, isVisible, onFontChanged, onWindowPaintFinished, postEvent, removeTimer, repaint, repaintNow, requestFocus, setBackColor, setBackForeColors, setFocusLess, setFont, setForeColor, setRect, setRect, setRect, setVisible
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, toString, wait, wait
 

Field Detail

items

protected Vector items

offset

protected int offset

selectedIndex

protected int selectedIndex

itemCount

protected int itemCount

visibleItems

protected int visibleItems

btnX

protected int btnX

sbar

protected ScrollBar sbar

simpleBorder

protected boolean simpleBorder
Constructor Detail

ListBox

public ListBox()
Creates an empty Listbox.

ListBox

public ListBox(Object[] items)
Creates a Listbox with the given items.
Method Detail

add

public void add(Object[] moreItems)
Adds an array of Objects to the Listbox

add

public void add(Object item)
Adds an Object to the Listbox

insert

public void insert(Object item,
                   int index)
Adds an Object to the Listbox at the given index

removeAll

public void removeAll()
Empties this ListBox, setting all elements of the array to 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 ('cos they use the same array reference), and you'll get a null pointer exception!

remove

public void remove(int itemIndex)
Removes the given index from the Listbox

remove

public void remove(Object item)
Removes an Object from the Listbox

setItemAt

public void setItemAt(int i,
                      Object s)
Sets the Object at the given Index, starting from 0

getItemAt

public Object getItemAt(int i)
Get the Object at the given Index

getSelectedItem

public Object getSelectedItem()
Returns the selected item of the Listbox or an empty Object if none is selected

getSelectedIndex

public int getSelectedIndex()
Returns the position of the selected item of the Listbox or -1 if the listbox has no selected index yet.

getItems

public Object[] getItems()
Returns all items in this ListBox. If the elements are Strings, the array can be casted to String[].

indexOf

public int indexOf(Object name)
Returns the index of the item specified by the name

select

public void select(int i)
Select an item and scroll to it if necessary. Note: select must be called only after the control has been added to the container and its rect has been set.

size

public int size()
Returns the number of items

add

public void add(Control control)
do nothing
Overrides:
add in class Container

remove

public void remove(Control control)
do nothing
Overrides:
remove in class Container

getPreferredWidth

public int getPreferredWidth()
returns the preferred width, ie, the size of the largest item plus 20.
Overrides:
getPreferredWidth in class Control

getPreferredHeight

public int getPreferredHeight()
returns the number of items multiplied by the font metrics height
Overrides:
getPreferredHeight in class Control

onBoundsChanged

protected void onBoundsChanged()
Description copied from class: Control
Called after an setRect.
Overrides:
onBoundsChanged in class Control

find

protected void find(char c)
Searches this ListBox for an item with the first letter matching the given char. The search is made case insensitive. Note: if you override this class you must implement this method.

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
Tags copied from class: Control
Parameters:
event - the event to process
See Also:
Event, KeyEvent, PenEvent

setEnabled

public void setEnabled(boolean enabled)
Description copied from class: Container
sets if this container and all childrens can or not accept events
Overrides:
setEnabled in class Container

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

onPaint

public void onPaint(Graphics g)
Description copied from class: Container
Draws the border (if any). If you override this method, be sure to call super.onPaint(g);, or the border will not be drawn.
Overrides:
onPaint in class Container
Tags copied from class: Control
Parameters:
g - the graphics object for drawing
See Also:
Graphics

setCursorColor

public void setCursorColor(Color color)
Sets the cursor color for this ListBox. The default is equal to the background slightly darker. Make sure you tested it in 2,4 and 8bpp devices.

drawItem

protected void drawItem(Graphics g,
                        int index,
                        int dx,
                        int dy)
You can extend ListBox and overide this method to draw the items

getItemWidth

protected int getItemWidth(int index)
Returns the width of the given item index with the current fontmetrics. Note: if you overide this class you must implement this method.

drawCursor

protected void drawCursor(Graphics g,
                          int sel,
                          boolean on)

setSimpleBorder

public void setSimpleBorder(boolean simpleBorder)
Sets the border of the listbox to be not 3d if flag is true.

qsort

public void qsort()
Sorts the elements of this ListBox. The current selection is cleared.