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 the user to choose a single selection or to allow
multiple choices. Usually, only a small group of choices are
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. He or 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.
A combo box is a cross-breed between a text field and a list. It displays a single line of text (possibly with an image) and a downward pointing arrow at 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 every other component 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 may 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. Figure 14.2 shows the example; the code itself follows.
/file: Lister.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Lister { public static void main(String[] args) { JFrame f = new JFrame("Lister v1.0"); f.setSize(200, 200); f.setLocation(200, 200); f.addWindowListener(new WindowAdapter( ) { public void windowClosing(WindowEvent we) { System.exit(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 (int i = 0; i < selection.length; i++) System.out.println(selection[i]); } }); // put the controls the content pane Container c = f.getContentPane( ); JPanel comboPanel = new JPanel( ); comboPanel.add(comboBox); c.add(comboPanel, BorderLayout.NORTH); c.add(new JScrollPane(list), BorderLayout.CENTER); c.add(button, BorderLayout.SOUTH); f.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. The list is 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, or drawings, or some combination of text and images.
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 of the items in the drop-down list. With an editable combo box, the user can type in a selection, as if it were a text field. Non-editable 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 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 or
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 the
next
chapter.
Get Learning Java 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.