It performs basically the same function as a list but

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ox A combo box is a simple list of items from which the user can combo choose. It performs basically the same function as a list, but can get only one value. javax.swing.JComponent javax.swing.JComboBox +JComboBox() +JComboBox(items: Object) +addItem(item: Object): void +getItemAt(index: int): Object +getItemCount(): int +getSelectedIndex(): int +setSelectedIndex(index: int): void +getSelectedItem(): Object +setSelectedItem(item: Object): void Creates a default empty combo box. Creates a combo box that contains the elements in the specified array. Adds an item to the combo box. Returns the item at the specified index. Returns the number of items in the combo box. Returns the index of the selected item. Sets the selected index in the combo box. Returns the selected item. Sets the selected item in the combo box. +removeItem(anObject: Object): void Removes an item from the item list. Removes the item at the specified index in the combo box. +removeItemAt(anIndex: int): void +removeAllItems(): void Removes all items in the combo box. EE3206/EE5805 Java Programming & Applications 28 JComboBox Methods and Event Handler To add an item to a JComboBox jcbo, use JComboBox To jcbo.addItem(Object item) To get an item from JComboBox jcbo, use JComboBox To jcbo.getItem() When a choice is checked or unchecked, the itemStateChanged() handler for ItemEvent is invoked as well as the actionPerformed() handler for ActionEvent. public void itemStateChanged(ItemEvent e) { // Make sure the source is a combo box if (e.getSource() instanceof JComboBox) String s = (String)e.getItem(); } EE3206/EE5805 Java Programming & Applications 29 Example: Using Combo Boxes This example lets users view an image and a description of a country's flag by selecting the country from a combo box. ComboBoxDemo Run EE3206/EE5805 Java Programming & Applications 30 JList A list is a component that performs basically the same function as a combo list box, but it enables the user to choose a single value or multiple values. javax.swing.JComponent javax.swing.JList +JList() +JList(items: Object) +getSelectedIndex(): int +setSelectedIndex(index: int): void +getSelectedIndices(): int Creates a default empty list. Creates a list that contains the elements in the specified array. Returns the index of the first selected item. Selects the cell at the specified index. Returns an array of all of the selected indices in increasing order. +setSelectedIndices(indices: int): void Selects the cells at the specified indices....
View Full Document

This note was uploaded on 01/11/2011 for the course EE 3206 taught by Professor Cwting during the Spring '07 term at City University of Hong Kong.

Ask a homework question - tutors are online