Java Reference
In-Depth Information
SR 11.22 Identify the class(es) and the line(s) of code from the class(es) that
provide each of the following for the Lightbulb program.
a. The background is black.
b. The message "Turn it off!" appears when you put the mouse
over the off button.
c. An off bulb will turn on if the user enters ALT-n.
d. The on button is grayed out when the bulb is turned on.
e. Originally the bulb is on.
11.8   Combo Boxes
A combo box allows the user to select one of several options
from a “drop down” menu. When the user presses a combo
box using the mouse, a list of options is displayed from
which the user can choose. The current choice is displayed in
the combo box. A combo box is defined by the JComboBox class.
A combo box can be either editable or uneditable. By default, a combo box is
uneditable. Changing the value of an uneditable combo box can be accomplished
only by selecting an item from the list. If the combo box is editable, however, the
user can change the value either by selecting an item from the list or by typing a
particular value into the combo box area.
The options in a combo box list can be established in one of two ways. We
can create an array of strings and pass it into the constructor of the JComboBox
class. Alternatively, we can use the addItem method to add an item to the combo
box after it has been created. A JComboBox can also display ImageIcon objects as
options as well.
The JukeBox program shown in Listing 11.11 demonstrates the use of a combo
box. The user chooses a song to play using the combo box and then presses the Play
button to begin playing the song. The Stop button can be pressed at any time to
stop the song. Selecting a new song while one is playing also stops the current song.
The JukeBoxControls class shown in Listing 11.12 is a panel that contains
the components that make up the jukebox GUI. The constructor of the class also
loads the audio clips that will be played. An audio clip is obtained first by creating
a URL object that corresponds to the wav or au file that defines the clip. The first
two parameters to the URL constructor should be "file" and "localhost" , respec-
tively, if the audio clip is stored on the same machine on which the program is exe-
cuting. Creating URL objects can potentially throw a checked exception; therefore
they are created in a try block. However, this program assumes the audio clips
will be loaded successfully and therefore does nothing if an exception is thrown.
KEY CONCEPT
A combo box provides a drop down
menu of options for the user.
 
Search WWH ::




Custom Search