Java Reference
In-Depth Information
This call tells our table (list) that we want to allow the user to select only
a single row or column at a time. Valid options are:
ListSelectionModel.SINGLE_SELECTION
ListSelectionModel.SINGLE_INTERVAL_SELECTION
ListSelectionModel.MULTIPLE_INTERVAL_SELECTION
The latter two allow the user to select more than one row at a time; multi-
ple intervals mean that the selected rows can be discontinuous. (Think
“Shift+click” versus “Control+click” as the user action that selects these.)
So what will our program do, once the user has made a selection? The se-
lected row is a subaccount of the current account and we will allow the user to
display that account and its subaccount, if any. Think of it as “changing
directory” into that account, to look at or change its status.
For a table to take an action when a selection is made you need another
listener called a selection listener . We wrote:
196 list.getSelectionModel().addListSelectionListener(
197 new ListSelectionListener()
198 {
199 public void
200 valueChanged(ListSelectionEvent e)
201 {
202 ListSelectionModel lsm =
(ListSelectionModel)e.getSource();
203 if (lsm.isSelectionEmpty()) {
204 view.setEnabled(false);
205 } else {
206 view.setEnabled(true);
207 }
208 } // valueChanged
209 }
210 );
Similar to how a table has a table model behind it, it also has a selection
model behind it. We don't need to reimplement an entire selection model; we
just retrieve the default one from our table ( list.getSelectionModel() )
and add a listener to it so that it will notify us when something has changed.
The javax.swing.event.ListSelectionListener is an interface with
only one method, so it's easy to extend and override it in place, as we do, begin-
ning at line 197. When called, it will be handed an event ( e ) and we take the
source of that event and coerce it to a ListSelectionModel . That's safe to
Search WWH ::




Custom Search