ListBox Class

ListBox Class

This Package | All Packages

Component
  |
  +--Control
    |
    +--ListBox
public class ListBox
extends Control

Encapsulates the Windows list box control.

Fields
Name Description
NOMATCHES While doing a search, if no matches are found, this is returned

Constructors
Name Description
ListBox() Creates a basic win32 list box with default values for everything.

Methods
Name Description
addItem(Object item) Adds an item to the list box.
addOnDrawItem(DrawItemEventHandler value) Adds an event handler for the OnDrawItem event notification.
addOnMeasureItem(MeasureItemEventHandler value) Adds an event handler for the OnMeasureItem event notification.
addOnSelectionChange(EventHandler value) Adds an event handler for the OnSelectionChange event notification.
findString(String findString) Finds the first item in the list that starts with the specified string.
findString(String findString, int indexStart) Finds the first item after the specified index that starts with the specified string.
findStringExact(String findString) Finds the first item in the list that is the same string as that specified in the findString parameter.
findStringExact(String findString, int indexStart) Finds the first item after the speicified index that is the same string as that specified in the findString parameter.
getBorder() Retrieves the current border setting.
getColumnWidth() In a multicolumn list box, returns the current width of each column in the list.
getCreateParams() getDrawMode
public final int getDrawMode();

Returns the style of the list box.

getDrawMode() Indicates if the list box should avoid showing partial items.
getIntegralHeight() Returns the item at the given index.
getItem(int index) Returns a count of the total number of items in the list box.
getItemCount() Returns the height of an item in the list box if the drawMode is either NORMAL or OWNERDRAWFIXED.
getItemHeight() Returns the height of the given item in an OWNERDRAWVARIABLE style list box.
getItemHeight(int index) Returns a Rectangle object which describes the bounding rectangle around an item in the list.
getItemRect(int index) Returns an object array with all the items in the ListBox.
getItems() Indicates if the list box is multicolumn or not.
getMultiColumn() Returns the total height of the items in the list box.
getPreferredHeight() For normal list boxes, this indicates if the scroll bar will always be shown on the right side of the list, even if the number of list items is not enough to fill the entire list.
getScrollAlwaysVisible() Indicates whether the item at the supplied index is selected.
getSelected(int index) Returns the index of the currently selected item in the list if there is one.
getSelectedIndex() If multiselection is allowed, returns an array of all items that are currently selected.
getSelectedIndices() Returns the value of the currently selected item in the list,if there is one.
getSelectedItem() Controls how many items in the list box can be selected at a time.
getSelectionStyle() Indicates if the list box is sorted or not.
getSorted() Retrieves the index of the first visible item in a list box.
getTopIndex() Enables a list box to recognize and expand tab characters when drawing its strings.
getUseTabStops() Allows you to add an Item to the ListBox in a location you specify, as opposed to the end of the list.
insertItem(int index, Object item) Returns the index of the item at the given coordinates.
itemFromPoint(Point p) Returns the index of the item at the given coordinates.
itemFromPoint(int x, int y) Override this to guarantee that the list is refilled when necessary.
recreateHandle() Removes all items from the list box.
removeAll() Removes an individual item from the list box at the supplied index.
removeItem(int index) Removes the given handler for the OnDrawItem event.
removeOnDrawItem(DrawItemEventHandler value) Removes the given handler for the OnMeasureItem event.
removeOnMeasureItem( MeasureItemEventHandler value) Removes the given handler for the OnSelectionChange event.
removeOnSelectionChange(EventHandler value) Sets the current border setting.
setBorder(boolean value) Sets the width of each column in a multicolumn list box.
setColumnWidth(int value) Sets the style of the list.
setDrawMode(int value) Indicates if the list box should avoid showing partial items.
setIntegralHeight(boolean value) Allows you to change the value at the given index.
setItem(int index, Object item) Sets the height of an item in an OWNERDRAWFIXED list box.
setItemHeight(int value) Allows the user to set all the items in the list box at once by passing in an array with all their values.
setItems(Object[] value) Indicates if the list box is multicolumn.
setMultiColumn(boolean value) For normal listboxes, indicates if the scroll bar will always be shown on the right side of the list, even if the number of list items is not enough to fill up the entire list.
setScrollAlwaysVisible(boolean value) Allows the user to set an item as being selected or not.
setSelected(int index, boolean value) Sets the item at the supplied index as being the selected item in the list.
setSelectedIndex(int index) Controls how many items in the list box can be selected at a time.
setSelectionStyle(int value) When the list box is sorted, all values in the list are sorted alphbaetically.
setSorted(boolean value) Enables a list box to recognize and expand tab characters when drawing its strings.
setUseTabStops(boolean value)

Fields

ListBox.NOMATCHES

Syntax
public static final int NOMATCHES;
Description
While doing a search, if no matches are found, this is returned

Constructors

ListBox.ListBox

Syntax
public ListBox();
Description

Creates a basic win32 list box with default values for everything.

Methods

ListBox.addItem

Syntax
public final int addItem( Object item );
Parameters
item
Object-typed value that we'll call toString() on to get the text to insert into the list box.
Return Value

Returns index of where the item was inserted.

Description

Adds an item to the list box. The code that adds the item will call toString() on it and inserts that value into the list box. If the sorted property is set to true, the item is properly sorted in the list. The actual index where the item was inserted is returned to the caller.

ListBox.addOnDrawItem

Syntax
public final void addOnDrawItem( DrawItemEventHandler value );
Parameters
value
New Event Handler to install for this event.
Description

Adds an event handler for the OnDrawItem event notification. This event is called for list boxes whose drawMode property is set to DrawMode.OWNERDRAWFIXED or DrawMode.OWNERDRAWVARIABLE and contains a Graphics object in which people should paint the item. Duplicates adds are not filtered out.

ListBox.addOnMeasureItem

Syntax
public final void addOnMeasureItem( MeasureItemEventHandler value );
Parameters
value
New Event Handler to install for this event.
Description

Adds an event handler for the OnMeasureItem event notification. This event is called for list boxes whose drawMode property is set to DrawMode.OWNERDRAWFIXED or DrawMode.OWNERDRAWVARIABLE and asks the programmer for the size of the given item in the list box. If the DrawMode is OWNERDRAWFIXED, this event is sent only once. Duplicates adds are not filtered out.

ListBox.addOnSelectionChange

Syntax
public final void addOnSelectionChange( EventHandler value );
Parameters
value
New Event Handler to install for this event.
Description

Adds an event handler for the OnSelectionChange event notification. This event is sent whenever the selection changes. Duplicates are not filtered out.

ListBox.findString

Syntax 1
public final int findString( String findString );
Parameters
findString
The string to match.
Return Value

Returns index of the item found, or returns ListBox.NOMATCHES if no match is found

Description

Finds the first item in the list that starts with the specified string. The search is case insensitive



Syntax 2
public final int findString( String findString, int indexStart );
Parameters
findString
The string to match.
indexStart
The index to start at. A -1 means start from the beginning.
Return Value

Returns an index of the item found, or returns ListBox.NOMATCHES if no match is found.

Description

Finds the first item after the specified index that starts with the specified string. This search is case insensitive.

ListBox.findStringExact

Syntax 1
public final int findStringExact( String findString );
Parameters
findString
The string to match in its entirety.
Return Value

Returns an index of the item found, or returns ListBox.NOMATCHES if no match is found.

Description

Finds the first item in the list that is the same string as that specified in the findString parameter. The search is still, however, case insensitive.



Syntax 2
public final int findStringExact( String findString, int indexStart );
Parameters
findString
The string to match in its entirety.
indexStart
The index to start at. A -1 means start from the beginning.
Return Value

Returns an index of the item found, or returns ListBox.NOMATCHES if no match is found.

Description

Finds the first item after the speicified index that is the same string as that specified in the findString parameter. The search is still, however, case insensitive.

ListBox.getBorder

Syntax
public boolean getBorder();
Return Value

Returns border setting.

Description

Retrieves the current border setting. If the border is set to true a 3D border is drawn around the list box.

See Also
setBorder

ListBox.getColumnWidth

Syntax
public final int getColumnWidth();
Return Value

Returns int The width, in pixels, of each column in the list box.

Description

In a multicolumn list box, returns the current width of each column in the list. A value of zero [0] means that Windows will choose a default width for each column.

ListBox.getCreateParams

Syntax
protected CreateParams getCreateParams();
Description

ListBox.getDrawMode

Syntax
public final int getDrawMode();
Return Value

Returns integer with the style.

Description

Returns the style of the list box. This indicates if the system draws it, or if the user paints each item manually. It also indicates whether items have to be of the same height.

ListBox.getIntegralHeight

Syntax
public final boolean getIntegralHeight();
Return Value

Returns boolean value indicating if only full items will be shown.

Description

Indicates if the list box should avoid showing partial items. If so, only full items will be displayed, and the list box will be resized to prevent partial items from being shown. Otherwise, they will be shown.

ListBox.getItem

Syntax
public final Object getItem( int index );
Parameters
index
Integer value with the index of the item to retrieve.
Return Value

Returns an Object handle with the item at that index in the list box.

Description

Returns the item at the given index. All indexes are zero-based.

ListBox.getItemCount

Syntax
public final int getItemCount();
Return Value

Returns the number of items in the list box.

Description

Returns a count of the total number of items in the list box.

ListBox.getItemHeight

Syntax 1
public final int getItemHeight();
Return Value

Returns an integer with the height, in pixels, of an item in the list.

Description

Returns the height of an item in the list box if the drawMode is either NORMAL or OWNERDRAWFIXED. For OWNERDRAWVARIABLE type list boxes, this routine returns the height of the first item. To determine the height of other items, use the other version of this routine.



Syntax 2
public final int getItemHeight( int index );
Parameters
index
Item to return the height for.
Return Value

Returns an integer with the height, in pixels, of the given item in the list.

Description

Returns the height of the given item in an OWNERDRAWVARIABLE style list box. This routine should not be called for fixed-item height style lists.

ListBox.getItemRect

Syntax
public final Rectangle getItemRect( int index );
Parameters
index
An index of item to return bounding rectangle for.
Return Value

Returns a rectangle.

Description

Returns a Rectangle object which describes the bounding rectangle around an item in the list.

ListBox.getItems

Syntax
public final Object[] getItems();
Return Value

Returns Object[]. The first item is the 0th element, and the last item in the list is last in the array.

Description

Returns an object array with all the items in the ListBox.

ListBox.getMultiColumn

Syntax
public final boolean getMultiColumn();
Return Value

Returns a boolean value indicating whether the list box is multicolumn.

Description

Indicates if the list box is multicolumn or not. If it's multicolumn, the list box uses the columnWidth property to determine how wide to make each column and displays the data horizontally in columns instead of vertically.

ListBox.getPreferredHeight

Syntax
public final int getPreferredHeight();
Return Value

Returns int with the height, in pixels, of all items in the list box.

Description

Returns the total height of the items in the list box.

ListBox.getScrollAlwaysVisible

Syntax
public final boolean getScrollAlwaysVisible();
Return Value

Returns a boolean value indicating whether the scroll bar should always be visible.

Description

For normal list boxes, this indicates if the scroll bar will always be shown on the right side of the list, even if the number of list items is not enough to fill the entire list. By default, Windows will show a scroll bar only when there are enough items to warrant it. For multicolumn list boxes, Windows does not show a horizontal scroll bar. Setting this value indicates that a horizontal scroll bar should be added. There is never a vertical scroll bar.

ListBox.getSelected

Syntax
public final boolean getSelected( int index );
Parameters
index
Indicates whether this item is selected or not.
Return Value

Returns a boolean value indicating whether it is true or not false.

Description

Indicates whether the item at the supplied index is selected.

ListBox.getSelectedIndex

Syntax
public final int getSelectedIndex();
Return Value

Returns an integer.

Description

Returns the index of the currently selected item in the list if there is one. If the value is -1, there is currently no selection. If the value is 0 or greater, the value is the index of the currently selected item. If the MultiSelect property on the list box is true, a non-zero value for this property is the index of the first selection

ListBox.getSelectedIndices

Syntax
public final int [] getSelectedIndices();
Return Value

Returns int [] Array with all the selected indexes.

Description

If multiselection is allowed, returns an array of all items that are currently selected. If in a single selection list box, then the currently selected item is returned in a single element array, or if the selection is not allowed, returns null.

ListBox.getSelectedItem

Syntax
public final Object getSelectedItem();
Return Value

Returns an object with either null or the current selection.

Description

Returns the value of the currently selected item in the list,if there is one. If the value is null, there is currently no selection. If the value is non-null, the value is that of the currently selected item. If the MultiSelect property on the list box is true, a non-null return value for this method is the value of the first item selected.

ListBox.getSelectionStyle

Syntax
public final int getSelectionStyle();
Return Value

Returns int with the current value.

Description

Controls how many items in the list box can be selected at a time. Valid values are: SelectionStyle.NONE [0] - No items can be selected in the list. SelectionStyle.ONE [1] - Only one item at a time can be selected. SelectionStyle.MULIT_SIMPLE [2] - More than one item at a time can be selected. SelectionStyle.MULTI_EXTENDED [3] - More than one item at a time can be selected and keyboard characters. such as SHIFT and CTRL can be used to assist in selection.

ListBox.getSorted

Syntax
public final boolean getSorted();
Return Value

Returns boolean with the current value.

Description

Indicates if the list box is sorted or not. If set to true, the strings in the list will be sorted alphabetically.

ListBox.getTopIndex

Syntax
public final int getTopIndex();
Return Value

Returns int Index of the topmost visible item.

Description

Retrieves the index of the first visible item in a list box. Initially, the item with index 0 is at the top of the list box, If the list box contents have been scrolled, another item may be at the top.

ListBox.getUseTabStops

Syntax
public final boolean getUseTabStops();
Return Value

Returns boolean indicating whether or not the list box will honor tabs.

Description

Enables a list box to recognize and expand tab characters when drawing its strings.

ListBox.insertItem

Syntax
public final int insertItem( int index, Object item );
Parameters
index
Where in the list [zero-based] it should be inserted.
item
The value of the item you want inserted.
Description

Allows you to add an Item to the ListBox in a location you specify, as opposed to the end of the list. Note that if the sorted property is set to true, values inserted by this method are not property sorted. The

See Also
addItem method should be used instead.

ListBox.itemFromPoint

Syntax 1
public final int itemFromPoint( Point p );
Parameters
point
Point object with the coordinates to test.
Return Value

Returns index of item that is under said point, or returns ListBox.NOMATCHES if there is no item in this location.

Description

Returns the index of the item at the given coordinates.



Syntax 2
public final int itemFromPoint( int x, int y );
Parameters
x
x coordinate of point to test.
y
y coordinate of point to test.
Return Value

Returns index of item that is at the specified point, or returns ListBox.NOMATCHES if there is no item in this location.

Description

Returns the index of the item at the given coordinates.

ListBox.recreateHandle

Syntax
public void recreateHandle();
Description

Override this to guarantee that the list is refilled when necessary.

ListBox.removeAll

Syntax
public void removeAll();
Description

Removes all items from the list box. All values and item data are lost.

ListBox.removeItem

Syntax
public final void removeItem( int index );
Parameters
index
Index of item to removed.
Description

Removes an individual item from the list box at the supplied index. The item and its values are discarded.

ListBox.removeOnDrawItem

Syntax
public final void removeOnDrawItem( DrawItemEventHandler value );
Parameters
value
Event handler to be removed.
Description

Removes the given handler for the OnDrawItem event. If there are duplicate entries, all are removed.

ListBox.removeOnMeasureItem

Syntax
public final void removeOnMeasureItem( MeasureItemEventHandler value );
Parameters
value
Event handler to be removed.
Description

Removes the given handler for the OnMeasureItem event. If there are duplicate entries, all are removed.

ListBox.removeOnSelectionChange

Syntax
public final void removeOnSelectionChange( EventHandler value );
Parameters
value
Event handler to be removed.
Description

Removes the given handler for the OnSelectionChange event. If there are duplicate entries, all are removed.

ListBox.setBorder

Syntax
public void setBorder( boolean value );
Parameters
value
New border setting.
Description

Sets the current border setting. If the border is set to true, a three-dimensisonal border is drawn around the list box.

See Also
setBorder

ListBox.setColumnWidth

Syntax
public final void setColumnWidth( int value );
Parameters
int
The new width of each column, in pixels.
Description

Sets the width of each column in a multicolumn list box. A value of zero [0] means that Windows will pick a width.

ListBox.setDrawMode

Syntax
public final void setDrawMode( int value );
Parameters
value
New value of the list box style.
Description

Sets the style of the list. This lets users control whether to paint the items themselves [default is not] and whether the items are all the same height [default yes]

ListBox.setIntegralHeight

Syntax
public final void setIntegralHeight( boolean value );
Parameters
value
The new value for this property.
Description

Indicates if the list box should avoid showing partial items. If so, only full items will be displayed, and the list box will be resized to prevent partial items from being shown. Otherwise, they will be shown.

ListBox.setItem

Syntax
public final void setItem( int index, Object item );
Parameters
index
Index of item to change the value for
item
New value for said index
Description

Allows you to change the value at the given index. If there is no value at the supplied index, an exception is thrown. Otherwise, the value becomes the newly supplied one

Exceptions

IllegalArgument thrown if the index is not currently assigned a value.

ListBox.setItemHeight

Syntax
public final void setItemHeight( int value );
Parameters
value
The height of an item in an OWNERDRAWFIXED list.
Description

Sets the height of an item in an OWNERDRAWFIXED list box. Users can use this instead of the OnMeasureItem event, which is best used by OWNERDRAWVARIABLE list boxes. This method is ignored by list boxes whose style is DrawMode.NORMAL

ListBox.setItems

Syntax
public final void setItems( Object[] value );
Parameters
value
Array of objects that will be inserted into the list box.
Description

Allows the user to set all the items in the list box at once by passing in an array with all their values. All old values in the list box are discarded and lost

ListBox.setMultiColumn

Syntax
public final void setMultiColumn( boolean value );
Parameters
value
New value indicating whether the list box is multi-column.
Description

Indicates if the list box is multicolumn. If it's multicolumn, the list box uses the columnWidth property to determine how wide to make each column and displays the data horizontally in columns instead of vertically.

ListBox.setScrollAlwaysVisible

Syntax
public final void setScrollAlwaysVisible( boolean value );
Parameters
value
The new value indicating if this should be set.
Description

For normal listboxes, indicates if the scroll bar will always be shown on the right side of the list, even if the number of list items is not enough to fill up the entire list. By default, Windows will show only a scroll bar when there are enough items to warrant it. For multicolumn list boxes, Windows does not show a horizontal scroll bar at all. Setting this value indicates that a horizontal scroll bar should be added. There is never a vertical scroll bar.

ListBox.setSelected

Syntax
public final void setSelected( int index, boolean value );
Parameters
index
Index of the item to mark as selected.
value
A boolean value indicating whether the item should be selected.
Description

Allows the user to set an item as being selected or not.

ListBox.setSelectedIndex

Syntax
public final void setSelectedIndex( int index );
Parameters
index
Index of item to select.
Description

Sets the item at the supplied index as being the selected item in the list. If the MultiSelect property is set to true, then this clears all other selections

ListBox.setSelectionStyle

Syntax
public final void setSelectionStyle( int value );
Return Value

Returns an integer with the current value.

Description

Controls how many items in the list box can be selected at a time. Valid values are: SelectionStyle.NONE [0] - No items can be selected in the list. SelectionStyle.ONE [1] - Only one item at a time can be selected. SelectionStyle.MULIT_SIMPLE [2] - More than one item at a time can be selected. SelectionStyle.MULTI_EXTENDED [3] - More than one item at a time can be selected, and keyboard characters such as SHIFT and CTRL can be used to assist in selection.

ListBox.setSorted

Syntax
public final void setSorted( boolean value );
Parameters
sorted
A boolean value indicating whether sorting should be on.
Description

When the list box is sorted, all values in the list are sorted alphbaetically. When the list box is not sorted, the values are displayed in the list in the order they were added.

ListBox.setUseTabStops

Syntax
public final void setUseTabStops( boolean value );
Parameters
value
New value indciating whether the list box should honor tab characters.
Description

Enables a list box to recognize and expand tab characters when drawing its strings.