JList
s and JComboBox
es are a step up on the evolutionary
chain from JButton
s and JLabel
s. Lists let the user choose from a group
of alternatives. They can be configured to force a single selection or
allow multiple choices. Usually, only a small group of choices is
displayed at a time; a scrollbar lets the user move to the choices that
aren’t visible. The user can select an item by clicking on it. She can
expand the selection to a range of items by holding down Shift and
clicking on another item. To make discontinuous selections, the user can
hold down the Control key instead of the Shift key (on a Mac, this is the
Command key).
A combo box is a crossbreed between a text field and a list. It displays a single line of text (possibly with an image) and a downward-pointing arrow on one side. If you click on the arrow, the combo box opens up and displays a list of choices. You can select a single choice by clicking on it. After a selection is made, the combo box closes up; the list disappears, and the new selection is shown in the text field.
Like other components in Swing, lists and combo boxes have data models that are distinct from visual components. The list also has a selection model that controls how selections can be made on the list data.
Lists and combo boxes are similar because they have similar data
models. Each is simply an array of acceptable choices. This similarity is
reflected in Swing, of course: the type of a JComboBox
’s data model is a subclass of the type
used for a JList
’s data model. The next
example demonstrates this relationship.
The following example creates a window with a combo box, a list, and a button. The combo box and the list use the same data model. When you press the button, the program writes out the current set of selected items in the list (see Figure 17-4).
Here’s the code for the example:
//file: Lister.java
import
java.awt.*
;
import
java.awt.event.*
;
import
javax.swing.*
;
public
class
Lister
{
public
static
void
main
(
String
[]
args
)
{
JFrame
frame
=
new
JFrame
(
"Lister v1.0"
);
// create a combo box
String
[]
items
=
{
"uno"
,
"due"
,
"tre"
,
"quattro"
,
"cinque"
,
"sei"
,
"sette"
,
"otto"
,
"nove"
,
"deici"
,
"undici"
,
"dodici"
};
JComboBox
comboBox
=
new
JComboBox
(
items
);
comboBox
.
setEditable
(
true
);
// create a list with the same data model
final
JList
list
=
new
JList
(
comboBox
.
getModel
());
// create a button; when it's pressed, print out
// the selection in the list
JButton
button
=
new
JButton
(
"Per favore"
);
button
.
addActionListener
(
new
ActionListener
()
{
public
void
actionPerformed
(
ActionEvent
ae
)
{
Object
[]
selection
=
list
.
getSelectedValues
();
System
.
out
.
println
(
"-----"
);
for
(
Object
o
:
selection
)
System
.
out
.
println
(
o
);
}
});
// put the controls the content pane
Container
c
=
frame
.
getContentPane
();
// unnecessary in 5.0+
JPanel
comboPanel
=
new
JPanel
();
comboPanel
.
add
(
comboBox
);
c
.
add
(
comboPanel
,
BorderLayout
.
NORTH
);
c
.
add
(
new
JScrollPane
(
list
),
BorderLayout
.
CENTER
);
c
.
add
(
button
,
BorderLayout
.
SOUTH
);
frame
.
setSize
(
200
,
200
);
frame
.
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
frame
.
setVisible
(
true
);
}
}
The combo box is created from an array of strings. This is a
convenience—behind the scenes, the JComboBox
constructor
creates a data model from the strings you supply and sets the JComboBox
to use that data model. Our list is
then created using the data model of the combo box. This works because
JList
expects to use a
ListModel
for its data model, and the
ComboBoxModel
used by the JComboBox
is a subclass of ListModel
.
The button’s action event handler simply prints out the selected
items in the list, which are retrieved with a call to getSelectedValues()
. This
method actually returns an object array, not a string array. List and
combo box items, like many other things in Swing, are not limited to text.
You can use images, drawings, or some combination of text and images. We
simply print the result as a string.
You might expect that selecting one item in the combo box would select the same item in the list. In Swing components, selection is controlled by a selection model. The combo box and the list have distinct selection models; after all, you can select only one item from the combo box, while it’s possible to select multiple items from the list. Thus, while the two components share a data model, they have separate selection models.
We’ve made the combo box editable. By default, it would not be editable: the user could choose only one item in the drop-down list. With an editable combo box, the user can type in a selection, as if it were a text field. Noneditable combo boxes are useful if you just want to offer a limited set of choices; editable combo boxes are handy when you want to accept any input but offer some common choices.
There’s a great class tucked away in the last example that deserves
some recognition. It’s JScrollPane
. In Lister
, you’ll notice that we created one when
we added the List
to the main window.
JScrollPane
simply wraps itself around
another Component
and provides
scrollbars as necessary. The scrollbars show up if the contained Component
’s preferred size (as returned by
getPreferredSize()
) is greater than the
size of the JScrollPane
itself. In the
previous example, the scrollbars show up whenever the size of the List
exceeds the available space.
You can use JScrollPane
to wrap
any Component
, including components
with drawings, images, or complex user interface panels. We’ll discuss
JScrollPane
in more detail later in
this chapter, and we’ll use it frequently with the text components in
Chapter 18.
Get Learning Java, 4th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.