Java Reference
In-Depth Information
TABLE 12-12
Some Constructors of the
class
JComboBox
public
JComboBox()
//Creates a JComboBox with no items to select.
//Example: JComboBox selectionList = new JComboBox();
//
selectionList is created but has no selectable items.
public
JComboBox(Vector<?> v)
//Creates a JComboBox to display the elements
//in the vector provided as an input parameter.
//Example: JComboBox selectionList = new JComboBox(v);
//
selectionList points to the combo box that lists the
//
elements contained in Vector v.
public
JComboBox(Object[] o)
//Constructor: Creates a JComboBox that displays the
//elements in the object array provided as an input parameter.
//Example: JComboBox selectionList = new JComboBox(o);
//
selectionList points to the combo box that lists the
//
elements contained in the array o.
In the previous two sections, we created an applet that uses check boxes and radio buttons
to change the font and style of the text. In this section, we add a
JComboBox
to the
program so that the user can select a font from a list of font names, and apply that font to
the text.
To create a combo box, we first declare a reference variable as follows:
private
JComboBox fontFaceDD;
//Line 1
Next, we create an array of strings and initialize it with the list of font names. The
corresponding Java statement is:
private
String fontNames[] = {"Dialog", "Century Gothic",
"Courier", "Serif"};
//Line 2
Next,weusethevariable
fontFaceDD
, declared in Line 1, and the array of strings
fontNames
, created in Line 2, to create a combo box. Consider the following
statement:
1
2
fontFaceDD =
new
JComboBox(fontNames);
//Line 3
This statement creates the object
fontFaceDD
and initializes this object using the strings
in the array
fontNames
.
The object
fontFaceDD
has four items. When you click the combo box, it shows you
the four choices. You can control the number of choices shown by using the method
setMaximumRowCount
. For example, the statement:
Search WWH ::
Custom Search