Java Reference
In-Depth Information
// JComboBox comboBox = new JComboBox(elements);
// comboBox.setRenderer(renderer);
// frame.add(comboBox, BorderLayout.NORTH);
frame.setSize(300, 200);
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
The output of this example is shown in Figure 13-7.
Figure 13-7. Using a more complex list cell renderer
Tip When you create your own rendering components, you'll find it's best to start with the default list cell
renderer. This allows you to focus on the specific details you're interested in. Otherwise, you'll need to worry
about everything, such as the default selection foreground and background colors, and whether you've
remembered to make the component opaque. Of course, if you want to configure everything yourself, feel free
to do so.
Selecting JList Elements
By default, every JList component is in multiple-selection mode. This means that you can
select multiple elements within the component. How you select multiple elements depends on
the user interface you're employing. For instance, with the Ocean look and feel interface, Ctrl-
select (Ctrl key and left mouse button on a right-handed mouse) acts as a selection toggle, and
Shift-select acts as a means of range selection.
ListSelectionModel Interface and DefaultListSelectionModel Class
An implementation of the ListSelectionModel interface controls the selection mechanism for
a JList component. The interface definition, shown here, defines constants for different selection
modes and describes how to manage a list of ListSelectionListener objects. It also provides
the means to describe several selection intervals.
 
Search WWH ::




Custom Search