![]() |
The Java Developers Almanac 1.4 |
|
e784. Listening for Changes to the Selection in a JList ComponentWhen the set of selected items is changed, either by the user or programmatically, a list selection event is fired. // Create a list
String[] items = {"A", "B", "C", "D"};
JList list = new JList(items);
// Register a selection listener
list.addListSelectionListener(new MyListSelectionListener());
class MyListSelectionListener implements ListSelectionListener {
// This method is called each time the user changes the set of selected items
public void valueChanged(ListSelectionEvent evt) {
// When the user release the mouse button and completes the selection,
// getValueIsAdjusting() becomes false
if (!evt.getValueIsAdjusting()) {
JList list = (JList)evt.getSource();
// Get all selected items
Object[] selected = list.getSelectedValues();
// Iterate all selected items
for (int i=0; i<selected.length; i++) {
Object sel = selected[i];
}
}
}
}
e775. Setting the Dimensions of an Item in a JList Component e776. Setting a Tool Tip for an Item in a JList Component e777. Getting the Items in a JList Component e778. Adding and Removing an Item in a JList Component e779. Getting the Selected Items in a JList Component e780. Setting the Selected Items in a JList Component e781. Setting the Selection Mode of a JList Component e782. Arranging Items in a JList Component e783. Detecting Double and Triple Clicks on an Item in a JList Component e785. Listening for Changes to the Items in a JList Component © 2002 Addison-Wesley. |