Java Reference
In-Depth Information
fontFaceDD.setMaximumRowCount(3);
sets the number of choices to be shown to 3. Because there are four choices in the combo
box
fontFaceDD
and only three choices are shown, a vertical scroll bar appears to the
right of the box. You can scroll this bar to see and select the other choices.
When you click an item in the combo box, it generates an item event. To process
item events, we use the
interface
ItemListener
. As described in the previous
section,
the item event handler code is placed in the body of
the method
itemStateChanged
.
When the user selects an item from a combo box, the index of the selected item can be
obtained by using the method
getSelectedIndex()
. For example, the statement:
currentFontName = fontNames[fontFaceDD.getSelectedIndex()];
assigns the current font name to the string variable
currentFontName
.
Example 12-7 gives the complete program listing for this
JComboBox
example.
EXAMPLE 12-7
//Welcome program with check boxes, radio buttons, and combo box
import
java.awt.*;
import
java.awt.event.*;
import
javax.swing.*;
public class
GrandWelcomeFinal
extends
JApplet
implements
ItemListener
{
private int
intBold = Font.PLAIN;
private int
intItalic = Font.PLAIN;
private
Color currentColor = Color.black;
private
String currentFontName ="Courier";
private
JCheckBox boldCB, italicCB;
private
JRadioButton redRB, greenRB, blueRB;
private
ButtonGroup ColorSelectBGroup;
private
JComboBox fontFaceDD;
private
String[] fontNames
= {"Dialog", "Century Gothic",
"Courier", "Serif"};
public void
init()
{
Container c = getContentPane();
c.setLayout(
null
);
Search WWH ::
Custom Search