19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

648 Chapter 17 GUI Components<br />

javax.swing.JComponent<br />

javax.swing.JComboBox<br />

+JComboBox()<br />

+JComboBox(items: Object[])<br />

+addItem(item: Object): void<br />

+getItemAt(index: int): Object<br />

+getItemCount(): int<br />

+getSelectedIndex(): int<br />

+setSelectedIndex(index: int): void<br />

+getSelectedItem(): Object<br />

+setSelectedItem(item: Object): void<br />

+removeItem(anObject: Object): void<br />

+removeItemAt(anIndex: int): void<br />

+removeAllItems(): void<br />

+addActionEvent(listener:<br />

ActionListener): void<br />

+addItemListener(listener:<br />

ItemListener) : void<br />

Creates a default empty <strong>com</strong>bo box.<br />

Creates a <strong>com</strong>bo box that contains the elements in the specified array.<br />

Adds an item <strong>to</strong> the <strong>com</strong>bo box.<br />

Returns the item at the specified index.<br />

Returns the number of items in the <strong>com</strong>bo box.<br />

Returns the index of the selected item.<br />

Sets the selected index in the <strong>com</strong>bo box.<br />

Returns the selected item.<br />

Sets the selected item in the <strong>com</strong>bo box.<br />

Removes an item from the item list.<br />

Removes the item at the specified index in the <strong>com</strong>bo box.<br />

Removes all the items in the <strong>com</strong>bo box.<br />

Adds an ActionListener for this object.<br />

Adds an ItemListener for this object.<br />

FIGURE 17.5<br />

JComboBox enables you <strong>to</strong> select an item from a set of items.<br />

Listing 17.4 gives a program that lets users view an image and a description of a country’s<br />

flag by selecting the country from a <strong>com</strong>bo box, as shown in Figure 17.6.<br />

Combo box<br />

DescriptionPanel<br />

FIGURE 17.6 Information about a country, including an image and a description of its flag,<br />

is displayed when the country is selected in the <strong>com</strong>bo box.<br />

Here are the major steps in the program:<br />

1. Create the user interface.<br />

Create a <strong>com</strong>bo box with country names as its selection values. Create a<br />

DescriptionPanel object (the DescriptionPanel class was introduced in the preceding<br />

section). Place the <strong>com</strong>bo box in the north of the frame and the description panel<br />

in the center of the frame.<br />

2. Process the event.<br />

Create a listener <strong>to</strong> implement the itemStateChanged handler <strong>to</strong> set the flag title,<br />

image, and text in the description panel for the selected country name.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!