![]() |
The Java Developers Almanac 1.4Order this book from Amazon. |
e779. Getting the Selected Items in a JList ComponentThe following methods return the indices of the selected items:// To create a list, see e774 Creating a JList Component // Get the index of all the selected items int[] selectedIx = list.getSelectedIndices(); // Get all the selected items using the indices for (int i=0; i<selectedIx.length; i++) { Object sel = list.getModel().getElementAt(selectedIx[i]); } // Get the index of the first selected item int firstSelIx = list.getSelectedIndex(); // Get the index of the last selected item int lastSelIx = list.getMaxSelectionIndex(); // Determine if the third item is selected int index = 2; boolean isSel = list.isSelectedIndex(index); // Determine if there are any selected items boolean anySelected = !list.isSelectionEmpty();The following methods return the selected item objects: // Get the first selected item Object firstSel = list.getSelectedValue(); // Get all selected items without using indices Object[] selected = list.getSelectedValues();
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 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 e784. Listening for Changes to the Selection in a JList Component e785. Listening for Changes to the Items in a JList Component © 2002 Addison-Wesley. |