![]() |
|||
![]() ![]() |
![]() |
|
![]() |
The colors used for displaying selected elements can be set and queried using the setSelectionForeground and setSelectionBackground/getSelectionBackground methods. These properties are bound in the JList class. After displaying the list, the users selection will need to be queried at some point. The indexes of the selected items, or the values themselves, can be queried. The getSelectedIndex and getSelectedValue methods return the index or value of the selected item in the list. If multiple selections are allowed, the index of the first selected item, or its value, is returned. If there is not a selected item, the getSelectedIndex method returns -1, while the getSelectedValue method returns null. When multiple selection is enabled, the set of selected items indexes or the set of selected values can be queried with the getSelectedIndices or getSelectedValues methods. The getSelectedIndices method returns an array of the indices of the selected items in numerical order, from the lowest index to the highest. Both methods return null if the selection set is empty. If only the range of selected indices is required, the getMinSelectionIndex methods can be used. If there are no items selected, both methods return -1. Also, if only one item is selected, both methods return the same index. In many lists, there will be more data elements than can be displayed at one time. When this is the case, the list can be added to a JScrollPane. By default, the scroll pane will display scrollbars when it cannot display the entire contents of the list in its viewable area. A complete description of the JScrollPane class and its options is presented in Chapter 15, Scrolling Components. The main method of the ListTest application can be changed to create a list that scrolls, as shown in Listing 10.4. The setVisibileRowCount method was used to configure the list to display five elements. The getVisibleRowCount method returns the number of elements displayed in the list. The resulting list is shown in Figure 10.2. Listing 10.4 Placing a JList Instance in a JScrollPane Instance public static void main( String args[] ) { String[] data = {Apple, Orange, Grape, Banana, Mango, Pineapple, Peach, Pear }; JFrame frame = new ApplicationFrame( List Test ); JList list = new JList( data ); list.setVisibleRowCount( 5 ); JScrollPane scrollPane = new JScrollPane( list ); scrollPane.setBorder( BorderFactory.createLoweredBevelBorder() ); frame.getContentPane().add( scrollPane, BorderLayout.CENTER ); frame.pack(); frame.setVisible( true ); } // main
Once the list is in a scroll pane, you may need to programmatically alter which elements are visible in the viewport. The ensureIndexIsVisible method scrolls the view to force an element at the specified index to be displayed. If the index is already visible, the method doesnt alter the list. The range of indexes currently being displayed can be queried with the getFirstVisibleIndex and getLastVisibleIndex methods. The bounds of a range of cells in the list can also be queried with the getCellBounds method. The indexes of the first and last rows are passed to the method, and the Rectangle that bounds the rows containing the indices is returned. Instances of the JList class do not fire high-level mouse events. For example, you may want to fire an ActionEvent when a double-click is detected over a list element. This isnt built into the list, so it has to be programmed by users of the list. To aid in such development, the locationToIndex method is provided. This method takes a Point instance as a parameter to the method and returns the index of the element whose bounds contain that point. If the point isnt over an element in the list, -1 is returned. A double-click of the left mouse button on an item in a list can be detected with the following mouse listener: list.addMouseListener( new MouseAdapter() { public void mouseClicked( MouseEvent e ) { if( e.getClickCount() == 2 && !( e.getModifiers() == InputEvent.ALT_MASK ) && !( e.getModifiers() == InputEvent.META_MASK ) ) { int index = list.locationToIndex(e.getPoint()); if( 0 <= index ) System.out.println(Double-clicked on Item + index); } } } );>
|
![]() |
|