Lists and Combo Boxes - Con't
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.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.