Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Online Training

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
 

Using a TreeSet to provide a sorted JList


by MageLang Institute

[Magercise | API Docs | Short Course| Magercises]

Help is available for each task.



    Task 1

    Either start with the skeleton code or create a SortedListModel class. The class extends AbstractListModel.


    Shift click to save the file to your working directory.

    Task 2

    Create an instance variable of type SortedSet. Then, in the constructor create an instance of type TreeSet and assign it to the variable.

    Task 3

    At a minimum, the AbstractListModel class requires the getSize() and getElementAt() methods of the ListModel interface to be defined. Complete the stubs such that they get the size and element from the set saved in the prior step.


    Use the size() method of Set to complete getSize().

    Either walk to the appropriate position, or convert the set to an array using the toArray() method of Set to complete getElementAt().

    Task 4

    Besides implementing the methods of ListModel, the SortedListModel class provides several methods to access and alter the data model. Many of the methods are already completed. The following UML diagram shows the complete set of operations.


    If you are using the skeleton code, there is no task to perform here.

    Task 5

    Two methods in the SortedListModel skeleton are left to complete: firstElement() and lastElement(). These require the use of methods specific to the SortedSet interface to complete.


    Use the first() method of SortedSet to find the first element.

    Use the last() method of SortedSet to find the last element.

    Task 6

    In the Tester skeleton, the printAction() method needs to be completed. As the name may infer, its purpose is to display a list of the elements in the JList. Use an Iterator to display the elements in its data model. The data model is stored in the model variable, which is of type SortedListModel.


    Use the iterator() method of SortedListModel to get an Iterator.

    Task 7

    Compile your program and run the Tester program to see what happens. You can provide several values as command line arguments to initialize the contents. Try out several buttons on the user interface to make sure the SortedListModel works.


    java Tester One Two Three Four Five Six Seven Eight 
        Nine Ten
    

    Make sure the elements in the JList are sorted.

Copyright © 1999 MageLang Institute. All Rights Reserved.


[ This page was updated: 29-Sep-99 ]

Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-99 Sun Microsystems, Inc.
All Rights Reserved. Legal Terms. Privacy Policy.