![]() ![]() ![]() |
|
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
Using a TreeSet to provide a sorted JListby 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
Either start with the skeleton code or create a SortedListModel class. The class extends AbstractListModel.
Create an instance variable of type SortedSet. Then, in the constructor create an instance of type TreeSet and assign it to the variable.
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.
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. ![]()
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.
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.
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 SourceDemonstration
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 CourseCopyright © 1999 MageLang Institute. All Rights Reserved. |