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

[Help | API Docs | Short Course| Magercises]

By default, the JList component displays its element list unsorted. With the help of the TreeSet, you can make it sorted by providing your own implementation of the ListModel interface for storing the data.

This magercise has you create just such an implementation.

If you aren't familiar with the Swing component set, don't worry. The Tester program includes all the necessary code to create the user interface. You are only responsible for finishing up the data model implementation and adding some action behind some of the buttons in the user interface.

Skeleton Code

Tasks


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

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

  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.

  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.

  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.

  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.

  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.

Where help exists, the task numbers above are linked to the step-by-step help page.

Solution Source

Demonstration

When you run the Tester program, from the command line, with an argument list of One Two Three Four Five Six Seven Eight Nine Ten, you'll see the following screen:

Your output will differ if you provide different command line options, your sorted data model implementation is wrong, or you've added and removed entries beyond the initial set. The Tester program automatically creates the user interface for you. It will be used to test your sorted data model class.

Next Magercise Magercises Short Course

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.