ComboBox Class

ComboBox Class

This Package | All Packages

Control
  |
  +--ComboBox
public class ComboBox
extends Control

Encapsulates a standard Windows combo box. This class supports the three basic combo box styles: a drop-down combo box, including an editable text portion; a drop-down list, with a read-only text portion; and a simple combo box, which is a drop-down combo box that is always in a drop-down state, with editable text. To add items to the combo box, you pass in an array of objects. Internally, the ComboBox class calls the toString method on the objects you pass in and inserts them into the combo box.

Constructors
Name Description
ComboBox() Creates a new ComboBox control with a default style of ComboBoxStyle.DROPDOWN.

Methods
Name Description
addItem(Object item) Adds an item to the combo box and retrieves the index of the new item.
addOnDrawItem(DrawItemEventHandler value) Adds an event handler for the onDrawItem event notification.
addOnDropDown(EventHandler value) Adds an event handler for the onDropDown 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.
beginUpdate() Temporarily disables any redrawing of the combo box.
endUpdate() Re-enables redrawing to a combo box (temporarily suspended by a previous call to beginUpdate.
findItem(Object item)

Note This method is obsolete.

findString(String s) Retrieves the zero-based index of first occurrence of the an item that begins with the specified text.
findString(String s, int startIndex) Retrieves the zero-based index of the first occurrence of an item that begins with the specified text, beginning the search at the specified index.
findStringExact(String s) Retrieves the zero-based index of the first occurrence of the specified item.
findStringExact(String s, int startIndex) Retrieves the zero-based index of the first occurrence of the specified item.
getCreateParams() Topic under construction.
getDrawMode() Retrieves the drawing mode of the combo box.
getDroppedDown() Retrieves a boolean indicating whether the drop-down portion of the combo box is currently displayed.
getItem(int index) Retrieves the item at the specified index.
getItemCount() Retrieves the number of items in the combo box.
getItemHeight() Retrieves the height of each item in the combo box.
getItemHeight(int index) Returns the height, based on the specified index, of the given item in a combo box in which the DrawMode.OWNERDRAWVARIABLE style is set.
getItems() Retrieves all the items in the combo box, and returns the items in an array of objects.
getMaxDropDownItems() Retrieves the maximum number of items that can be displayed in the drop-down portion of the combo box.
getMaxLength() Retrieves the maximum length of the text the user can type into the edit control of a combo box.
getSelectedIndex() Retrieves the index of the currently selected item in the combo box.
getSelectedItem() Retrieves the object that is currently selected in the combo box.
getSelectedText() Retrieves the text of the currently selected item.
getSelectionEnd() Retrieves the zero-based index of the last character in the combo box selection.
getSelectionStart() Retrieves the index of the first character of the currently selected text.
getSorted() Retrieves a boolean value that indicates whether the contents of the combo box are sorted.
getStyle() Retrieves the style of the combo box.
insertItem(int index, Object item) Inserts an item into the combo box at the given index.
removeAll() Clears the combo box.
removeItem(int index) Removes the item at the specified index.
removeOnDrawItem(DrawItemEventHandler value) Removes the specified handler for the onDrawItem event.
removeOnDropDown(EventHandler value) Removes the specified handler for the onDropDown event.
removeOnMeasureItem(MeasureItemEventHandler value) Removes the specified handler for the onMeasureItem event.
removeOnSelectionChange(EventHandler value) Removes the specified handler for the onSelectionChange event.
select(int start, int end) Selects the text in the specified range.
selectAll() Selects all the text in the editable portion of the combo box.
setDrawMode(int value) Sets the drawing style of the combo box.
setDroppedDown(boolean value) Displays or hides the drop-down portion of the combo box.
setItem(int index, Object item) Initializes the specified index.
setItemHeight(int value) Sets the height of an item in a combo box of style ComboBoxStyle.OWNERDRAWFIXED.
setItems(Object[] value) Initializes a combo box based on the contents of an array of objects.
setMaxDropDownItems(int value) Sets the maximum number of items that can be displayed in the dropdown portion of the combo box.
setMaxLength(int value) Sets the maximum allowable length of the text in the editable portion of the combo box.
setSelectedIndex(int index) Selects the specified item.
setSelectedText(String value) Replaces the currently selected text with the specified text.
setSelectionEnd(int value) Specifies the zero-based index of the last character of a text selection in the ComboBox control.
setSelectionStart(int value) Specifies the zero-based index of the first character of a text selection in the Edit control.
setSorted(boolean value) Specifies whether the items in the combo box should be sorted.
setStyle(int value) Sets the style of the combo box.

Constructors

ComboBox.ComboBox

Syntax
public ComboBox();
Description

Creates a new ComboBox control with a default style of ComboBoxStyle.DROPDOWN. To change the style of the ComboBox, use the setStyle method with one of the constants defined in the ComboBoxStyle class.

Methods

ComboBox.addItem

Syntax
public int addItem( Object item );
Parameters
item
The item to add to the ComboBox. Internally, the ComboBox class retrieves the string representation of this item by calling the object's toString method.
Return Value

Returns the zero-based index of the added item.

Description

Adds an item to the combo box and retrieves the index of the new item. For an unsorted combo box, the item is added to the end of the existing list of items. For a sorted combo box, the item is inserted into the list according to its sorted position.

Exceptions

RunTimeException thrown if there is insufficient space in which to store the new item.

ComboBox.addOnDrawItem

Syntax
public void addOnDrawItem( DrawItemEventHandler value );
Parameters
value
A DrawItemEventHandler object.
Description

Adds an event handler for the onDrawItem event notification. The onDrawItem event is fired for combo boxes whose draw mode setting is DrawMode.OWNERDRAWFIXED or DrawMode.OWNERDRAWVARIABLE. The DrawItemEventHandler object passed as a parameter to this event handler contains a Graphics object that you use to paint the item. Duplicate calls to the addOnDrawItem method are not filtered out by the ComboBox class.

ComboBox.addOnDropDown

Syntax
public void addOnDropDown( EventHandler value );
Parameters
value
The new event handler to install for this event.
Description

Adds an event handler for the onDropDown event notification. This event is fired when the drop-down portion of the combo box is displayed. Duplicate calls to addOnDropDown are not filtered out by the ComboBox class.

ComboBox.addOnMeasureItem

Syntax
public void addOnMeasureItem( MeasureItemEventHandler value );
Parameters
value
The new event handler to install for this event.
Description

Adds an event handler for the onMeasureItem event notification. This event is fired for list boxes whose drawMode property is set to DrawMode.OWNERDRAWFIXED or DrawMode.OWNERDRAWVARIABLE. Within this onMeasureItem event, you use the MeasureItemEvent object passed to the handler to notify Windows of the size of a given item in the combo box. If the DrawMode for this combo box is OWNERDRAWFIXED, this event is sent only once. Duplicate calls to addOnMeasureItem are not filtered out by the ComboBox class.

ComboBox.addOnSelectionChange

Syntax
public final void addOnSelectionChange( EventHandler value );
Parameters
value
The new event handler to install for this event.
Description

Adds an event handler for the onSelectionChange event notification. This event is fired when the user changes the selection in the combo box. Duplicate calls to this method are not filtered out by the ComboBox class.

ComboBox.beginUpdate

Syntax
public void beginUpdate();
Description

Temporarily disables any redrawing of the combo box. A call to beginUpdate must be followed by a call to endUpdate. Following the call to beginUpdate, any redrawing that would result by operations performed on the combo box is deferred until the call to endUpdate.

ComboBox.endUpdate

Syntax
public void endUpdate();
Description

Re-enables redrawing to a combo box (temporarily suspended by a previous call to beginUpdate. All redrawing of the combo box occurs after the call to endUpdate.

ComboBox.findItem

Syntax
public int findItem( Object item );
Description

Note This method is obsolete.

ComboBox.findString

Syntax 1
public int findString( String s );
Parameters
s
The index of the string to retrieve.
Return Value

Returns the zero-based index of the first occurrence of the specified item if the item exists; otherwise, returns -1.

Description

Retrieves the zero-based index of first occurrence of the an item that begins with the specified text. The search for this item starts at the beginning of the list of items in the combo box, and the search is case-insensitive.



Syntax 2
public int findString( String s, int startIndex );
Parameters
s
The string for which to search.
startIndex
The index at which to begin the search.
Return Value

Returns the zero-based index of the first item that begins with the specified text if it exists; otherwise, returns -1.

Description

Retrieves the zero-based index of the first occurrence of an item that begins with the specified text, beginning the search at the specified index. This search is case-insensitive.

ComboBox.findStringExact

Syntax 1
public int findStringExact( String s );
Parameters
s
The string for which to search.
Return Value

Returns the zero-based index of the first occurrence of the item if the item exists; therwise, returns -1.

Description

Retrieves the zero-based index of the first occurrence of the specified item. The search for the item begins at the beginning of the list of items stored by the combo box. The specified string must match exactly the string in the combo box, except in regard to case.



Syntax 2
public int findStringExact( String s, int startIndex );
Parameters
s
The string for which to search.
startIndex
The index of the item with which to begin the search.
Return Value

Returns the zero-based index of the first string (after the specified index) that matches the search string if the string exists; otherwise, returns -1.

Description

Retrieves the zero-based index of the first occurrence of the specified item. The search for the item begins at the specified index. The specified string must match the string in the combo box exactly, except in regard to case.

ComboBox.getCreateParams

Syntax
protected CreateParams getCreateParams();
Description

Topic under construction.

ComboBox.getDrawMode

Syntax
public int getDrawMode();
Return Value

Returns an enumeration constant defined DrawMode class.

Description

Retrieves the drawing mode of the combo box. The DrawMode property controls whether the control is drawn by Windows or by the user.

See Also
wfc.ui.DrawMode enumeration.

ComboBox.getDroppedDown

Syntax
public boolean getDroppedDown();
Return Value

Returns true if the drop-down portion of the combo box is displayed; otherwise, returns false.

Description

Retrieves a boolean indicating whether the drop-down portion of the combo box is currently displayed.

ComboBox.getItem

Syntax
public Object getItem( int index );
Parameters
index
The index of the item to retrieve.
Return Value

Returns the item at the index specified.

Description

Retrieves the item at the specified index.

Exceptions

InvalidArgument thrown if the specified index is less than zero or exceeds the number of items in the combo box.

ComboBox.getItemCount

Syntax
public int getItemCount();
Return Value

Returns an integer that specifies the number of items in the combo box.

Description

Retrieves the number of items in the combo box.

ComboBox.getItemHeight

Syntax 1
public int getItemHeight();
Return Value

Returns the height of the items in the combo box.

Description

Retrieves the height of each item in the combo box. For all DrawModes except OWNERDRAWVARIAIBLE, all items have the same height. For OWNERDRAWVARIABLE combo boxes, each item may have a different height.



Syntax 2
public final int getItemHeight( int index );
Parameters
index
The index of the item for which to return the height.
Return Value

Returns the height of the specified item in pixels,.

Description

Returns the height, based on the specified index, of the given item in a combo box in which the DrawMode.OWNERDRAWVARIABLE style is set. This method should not be called for combo boxes in which the item height is fixed.

ComboBox.getItems

Syntax
public Object[] getItems();
Return Value

Returns an array with all the items currently in the combo box.

Description

Retrieves all the items in the combo box, and returns the items in an array of objects.

ComboBox.getMaxDropDownItems

Syntax
public int getMaxDropDownItems();
Return Value

Returns the maximum number of items that can be displayed.

Description

Retrieves the maximum number of items that can be displayed in the drop-down portion of the combo box. This number can be between 1 and 100.

ComboBox.getMaxLength

Syntax
public int getMaxLength();
Return Value

Returns the maximum lengh of the text.

Description

Retrieves the maximum length of the text the user can type into the edit control of a combo box.

ComboBox.getSelectedIndex

Syntax
public int getSelectedIndex();
Return Value

Returns the zero-based index of the currently selected item.

Description

Retrieves the index of the currently selected item in the combo box.

ComboBox.getSelectedItem

Syntax
public Object getSelectedItem();
Return Value

Returns the currently selected object if an item is selected; otherwise, returns null.

Description

Retrieves the object that is currently selected in the combo box.

ComboBox.getSelectedText

Syntax
public String getSelectedText();
Return Value

Returns the text of the currently selected item.

Description

Retrieves the text of the currently selected item.

ComboBox.getSelectionEnd

Syntax
public int getSelectionEnd();
Return Value

Returns the zero-based index of the last character in the current selection.

Description

Retrieves the zero-based index of the last character in the combo box selection.

ComboBox.getSelectionStart

Syntax
public int getSelectionStart();
Return Value

Returns the zero-based index of the start of the first character in current selection.

Description

Retrieves the index of the first character of the currently selected text.

ComboBox.getSorted

Syntax
public boolean getSorted();
Return Value

Returns true if the list is sorted; otherwise, returns false.

Description

Retrieves a boolean value that indicates whether the contents of the combo box are sorted.

ComboBox.getStyle

Syntax
public int getStyle();
Return Value

Returns the style of the combo box.

Description

Retrieves the style of the combo box. The returned value is one of the enumeration constants defined in the ComboBoxStyle class.

ComboBox.insertItem

Syntax
public int insertItem( int index, Object item );
Parameters
index
The index at which to insert the item.
item
The item to insert.
Return Value

Returns the index at which the item was inserted.

Description

Inserts an item into the combo box at the given index.

ComboBox.removeAll

Syntax
public void removeAll();
Description

Clears the combo box.

ComboBox.removeItem

Syntax
public void removeItem( int index );
Parameters
index
The index of the item to remove.
Description

Removes the item at the specified index.

ComboBox.removeOnDrawItem

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

Removes the specified handler for the onDrawItem event. If there are duplicate entries, all the handlers are removed.

ComboBox.removeOnDropDown

Syntax
public void removeOnDropDown( EventHandler value );
Parameters
value
The event handler to be removed.
Description

Removes the specified handler for the onDropDown event. If there are duplicate entries, all the handlers are removed.

ComboBox.removeOnMeasureItem

Syntax
public void removeOnMeasureItem( MeasureItemEventHandler value );
Parameters
value
The event handler to remove.
Description

Removes the specified handler for the onMeasureItem event. If there are duplicate entries, all the handlers are removed.

ComboBox.removeOnSelectionChange

Syntax
public final void removeOnSelectionChange( EventHandler value );
Parameters
value
The event handler to remove.
Description

Removes the specified handler for the onSelectionChange event. If there are duplicate entries, all the handlers are removed.

ComboBox.select

Syntax
public void select( int start, int end );
Parameters
start
The zero-based index of the character with which to begin the selection.
end
The zero-based index of the character with which to end the selection.
Description

Selects the text in the specified range.

ComboBox.selectAll

Syntax
public void selectAll();
Description

Selects all the text in the editable portion of the combo box.

ComboBox.setDrawMode

Syntax
public void setDrawMode( int value );
Parameters
value
One of the enumeration constants defined in the DrawMode class.
Description

Sets the drawing style of the combo box. By setting the drawing mode of the combo box, you specify whether the combo box is owner-draw and whether all items displayed in the combo box are of the same height.

Exceptions

InvalidEnumArgument yhrown if the specified value is less than DrawMode.MIN or exceeds DrawMode.MAX.

ComboBox.setDroppedDown

Syntax
public void setDroppedDown( boolean value );
Parameters
value
If true, the drop-down portion of the combo box is displayed. Otherwise, it is hidden.
Description

Displays or hides the drop-down portion of the combo box.

ComboBox.setItem

Syntax
public void setItem( int index, Object item );
Parameters
index
The index at which to set the data.
item
The data to store at the specified index.
Description

Initializes the specified index. If the item at the specified index already contains data, this data is lost.

ComboBox.setItemHeight

Syntax
public void setItemHeight( int value );
Parameters
value
The height of items in the combo box.
Description

Sets the height of an item in a combo box of style ComboBoxStyle.OWNERDRAWFIXED. If the style of a combo box is DrawMode.NORMAL, a call to this method is ignored.

ComboBox.setItems

Syntax
public void setItems( Object[] value );
Parameters
value
The array of objects to insert.
Description

Initializes a combo box based on the contents of an array of objects. All values currently stored in the combo box are lost.

ComboBox.setMaxDropDownItems

Syntax
public void setMaxDropDownItems( int value );
Parameters
value
The maximum number of items that can be displayed.
Description

Sets the maximum number of items that can be displayed in the dropdown portion of the combo box. This value can be between 1 and 100.

Exceptions

IllegalArgumentException thrown if the specified value is less than zero or more than one hundred.

ComboBox.setMaxLength

Syntax
public void setMaxLength( int value );
Parameters
value
The maximum length of the text.
Description

Sets the maximum allowable length of the text in the editable portion of the combo box.

ComboBox.setSelectedIndex

Syntax
public void setSelectedIndex( int index );
Parameters
index
The zero-based index of the item to select.
Description

Selects the specified item.

ComboBox.setSelectedText

Syntax
public void setSelectedText( String value );
Parameters
value
The text with which to replace the currently selected text.
Description

Replaces the currently selected text with the specified text.

ComboBox.setSelectionEnd

Syntax
public void setSelectionEnd( int value );
Parameters
value
The zero-based index of the character at which the selection ends.
Description

Specifies the zero-based index of the last character of a text selection in the ComboBox control.

ComboBox.setSelectionStart

Syntax
public void setSelectionStart( int value );
Parameters
value
The zero-based index of the first character at which the selection begins.
Description

Specifies the zero-based index of the first character of a text selection in the Edit control.

ComboBox.setSorted

Syntax
public void setSorted( boolean value );
Parameters
value
Set to true if the items should be sorted; otherwise, set to false.
Description

Specifies whether the items in the combo box should be sorted. By default, items in a ComboBox control are not sorted.

ComboBox.setStyle

Syntax
public void setStyle( int value );
Parameters
value
The style of the combo box. This value must be one of the enumeration constants defined in the ComboBoxStyle class.
Description

Sets the style of the combo box.