Skip to main content
IBM 
ShopSupportDownloads
IBM HomeProductsConsultingIndustriesNewsAbout IBM
IBM : developerWorks : Java : Education - online courses
Java Collections Framework
Download tutorial zip fileView letter-sized PDF fileView A4-sized PDF fileE-mail this tutorial to a friend
Main menuSection menuGive feedback on this tutorialPreviousNext
9. Exercises
  


Exercise 2. How to use a TreeSet to provide a sorted JList page 4 of 9


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 exercise 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

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

Help for task 1:
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.

Help for task 3:
Use the size() method of Set to complete getSize().

Either iterate through the set 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.

Help for task 4:
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.

Help for task 5:
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 imply, 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.

Help for task 6:
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.

Help for task 7:


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

Make sure the elements in the JList are sorted.


Main menuSection menuGive feedback on this tutorialPreviousNext
PrivacyLegalContact