Java Reference
In-Depth Information
JComboBox facenameCombo = new JComboBox();
facenameCombo.addItem("Serif");
facenameCombo.addItem("SansSerif");
. . .
792
793
Figure 5
An Open Combo Box
You get the item that the user has selected by calling the getSelectedItem
method. However, because combo boxes can store other objects in addition to
strings, the getSelectedItem method has return type Object . Hence you
must cast the returned value back to String .
String selectedString
= (String) facenameCombo.getSelectedItem();
You can select an item for the user with the setSelectedItem method.
Radio buttons, check boxes, and combo boxes generate action events, just as
buttons do.
Radio buttons, check boxes, and combo boxes generate an ActionEvent
whenever the user selects an item. In the following program, we don't care which
component was clickedȌall components notify the same listener object. Whenever
the user clicks on any one of them, we simply ask each component for its current
content, using the isSelected and getSelectedItem methods. We then
redraw the text sample with the new font.
Figure 6 shows how the components are arranged in the frame. Figure 7 shows the
UML diagram.
Search WWH ::




Custom Search