< Day Day Up > |
List selection events occur when the selection in a list or table is either changing or has just changed. The events are fired from an object that implements the ListSelectionModel interface. [19] To get a list or table's list selection model object, use the getSelectionModel method.
To detect list selection events, you register a listener on the appropriate list selection model object. The JList class also gives you the option of registering a listener on the list itself, rather than directly on the list selection model. This section looks at an example that shows how to listen to list selection events on a selection model. Examples That Use List Selection Listeners (page 688) lists examples that listen on the list directly. The selection model is shared by a list and a table. Figure 9 is a picture of the example running. You can dynamically change the selection mode to any of the three supported modes:
Figure 9. The ListSelectionDemo application.
Try This:
Here's the code from ListSelectionDemo.java that sets up the selection model and adds a listener to it: ...//where the member variables are defined JList list; JTable table; ...//in the init method: listSelectionModel = list.getSelectionModel(); listSelectionModel.addListSelectionListener( new SharedListSelectionHandler()); ... table.setSelectionModel(listSelectionModel); And here's the code for the listener, which works for all the possible selection modes: class SharedListSelectionHandler implements ListSelectionListener { public void valueChanged(ListSelectionEvent e) { ListSelectionModel lsm = (ListSelectionModel)e.getSource(); int firstIndex = e.getFirstIndex(); int lastIndex = e.getLastIndex(); boolean isAdjusting = e.getValueIsAdjusting(); output.append("Event for indexes " + firstIndex + " - " + lastIndex + "; isAdjusting is " + isAdjusting + "; selected indexes:"); if (lsm.isSelectionEmpty()) { output.append(" <none>"); } else { // Find out which indexes are selected. int minIndex = lsm.getMinSelectionIndex(); int maxIndex = lsm.getMaxSelectionIndex(); for (int i = minIndex; i <= maxIndex; i++) { if (lsm.isSelectedIndex(i)) { output.append(" " + i); } } } output.append(newline); } } This valueChanged method displays the first and last indices reported by the event, the value of the event's isAdjusting flag, and the indices currently selected. Note that the first and last indices reported by the event indicate the inclusive range of items for which the selection has changed. If the selection mode is multiple interval selection, some items within the range might not have changed. The isAdjusting flag is true if the user is still manipulating the selection, and false if the user has finished changing it. The ListSelectionEvent [21] object passed into valueChanged indicates only that the selection has changed. The event contains no information about the current selection. So, this method queries the selection model to figure out the current selection.
The List Selection Listener APITable 22 lists the methods in the ListSelectionListener interface and Table 23 describes the methods in the ListSelectionEvent class. Note that because ListSelectionListener has only one method, it has no corresponding adapter class. Also refer to the List- SelectionListener API documentation at: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/event/ListSelectionListener.html. The ListSelectionEvent API documentation is online at: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/event/ListSelectionEvent.html. Table 22. The ListSelectionListener Interface
Table 23. The ListSelectionEvent API
Examples That Use List Selection ListenersThe following examples use list selection listeners.
|
< Day Day Up > |