Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


The Selection Model

As shown earlier in this chapter, a list supports multiple modes for selecting items in it. However, the selection isn’t managed by the list itself. Just as the data is contained in a separate data model, a separate selection model manages selection. The responsibilities of a list section model are defined by the ListSelectionModel interface. A class that implements this interface is used to manage selection in the list. The JFC contains the concrete class DefaultListSelectionModel, which can be used in a list. In fact, the methods for setting the selection mode and altering the selection set for the list, presented earlier in the chapter, are simple wrapper methods that forward the request to the current selection model.

Implementing the ListSelectionModel interface is far less common than implementing the ListModel interface. In most cases, the DefaultListSelectionModel can be used. However, there are still situations where you want to obtain a handle on a list selection model, such as to allow multiple lists to share a single ListSelectionModel. This way, when the selection changes in one list, it will also change in the lists that share the common model.

Listing 10.9 contains a simple application to present the idea of a common ListSelectionModel. The ListSelectionModelTest application creates two lists that share a single ListSelectionModel. When an element in either list is selected or deselected, the corresponding element in the other list is also selected or deselected (Figure 10.4).

Listing 10.9 The ListSelectionModelTest Application

package com.foley.test;

import java.awt.*;
import javax.swing.*;

import com.foley.utility.ApplicationFrame;

/**
 * An application that displays a few JList instances
 * that share the same ListSelectionModel.
 *
 * @author Mike Foley
 **/
public class ListSelectionModelTest extends Object {

    /**
     * Application entry point.
     * Create a frame, the lists and display it.
     * Set the selection model in each list to be the
     * same.
     *
     * @param args Command line parameter. Not used.
     **/
    public static void main( String args[] ) {
        String[] firstNames = { “Mike”, “Mari”, “Molly”,
                                “Betsy”, “Bobby” };
        String[] lastNames = { “Jones”, “Smith”, “Johnson”,
                               “White”, “Connor” };

        JFrame frame = new ApplicationFrame( “ListSelectionModel Test” );

        JList list1 = new JList( firstNames );
        JList list2 = new JList( lastNames );

        list1.setPrototypeCellValue( “Long First Name” );
        list2.setPrototypeCellValue( “Long Last Name” );

        ListSelectionModel selectionModel = list1.getSelectionModel();
        list2.setSelectionModel( selectionModel );

        JScrollPane scrollPane1 = new JScrollPane( list1 );
        JScrollPane scrollPane2 = new JScrollPane( list2 );

        scrollPane1.setBorder(BorderFactory.createLoweredBevelBorder());
        scrollPane2.setBorder(BorderFactory.createLoweredBevelBorder());

        frame.getContentPane().add( scrollPane1, BorderLayout.WEST );
        frame.getContentPane().add( scrollPane2, BorderLayout.EAST );

        frame.pack();
        frame.setVisible( true );

    } // main

} // ListSelectionModelTest


Figure 10.4  The ListSelection ModelTest application.

The ListSelectionModelTest application works very nicely because each ListModel contains the same number of elements. If the list models contain a different number of elements, a valid selection in one list may not be valid in the other. This can be demonstrated by changing the lastNames array to contain only three members, as in the following line of code:

String[] lastNames = { “Jones”, “Smith”, “Johnson” };

The new ListSelectionModelTest application is shown in Figure 10.5.


Figure 10.5  The ListSelection ModelTest application with non-symmetric lists.

Figure 10.5 shows that if the JList instance receives a selection event that is outside the number of elements in the list, it ignores that selected item. This enables views to share a single list model even if the data models are not the same size. It should be mentioned that classes other than JList that use a ListSelectionModel to manage selections may behave differently.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.