Chapter 2, Lists and Combos
#15 Make JLists Checkable
The general idea of this pop up can be modified to work in similar ways for
different kinds of searches, such as popping up a list of which field the
search is to be applied to if the list items are complex. An example of this is
a mail program in which the pop up might show the options Subject, To,
From, etc., meaning that the search is limited to finding the specified term in
only the selected field.
H A C K
Make JLists Checkable Hack #15
Avoid losing 50 selections to an unshifted click.
One horrible UI problem is dealing with vast collections of things that need
to be presented to the user and made selectable. If, like me, you’ve ever had
1,000 emails in your inbox, you know what I mean. Worse, what if you pick
a bunch of items to delete, but your finger slips off the key used for multi-
selection (Alt on Windows, Command on the Mac, etc.) and you lose all of
your previous selections? Overriding the native selection behavior can make
this situation somewhat more palatable.
Because a list like this behaves differently than a normal list, it should look
different, too, so I’ve opted for a checkbox metaphor. Each item is shown
with a checkbox, and as you click more items, they get checked, and if you
select an already-checked item, it gets unchecked.
This turns out to be a little harder than expected. I once did
JList, creating my own scrolling layout of JPanels
and faking the list behavior. It turned up a funny Swing bug
because I was using
GridBagLayout for the fake list, and it
started totally bombing out after about 500 items were
added to the list. This was because
GridBagLayout has a bug
where it can’t have more than 512 rows. Considering the bug
(number 4254022 on the Java Bug Parade) was filed in 1999
and is still open, I’m figuring it won’t get fixed by the time
you read this.
The basis of the checkable list is a JList. The tricky part here is that there
isn’t a way (that I’ve found) to steal the mouse clicks from the
consume them before the normal calls to the
ListSelectionModel are made.
Instead, the strategy is to set up a
ListSelectionListener and just fix every-
JList has done its thing.
To implement the checkbox functionality, subclass
JList and give it a cus-
ListSelectionListener and a ListCellRenderer. A complete listing is
shown in Example 2-6.