![]() ![]() ![]() |
|
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
Using a TreeSet to provide a sorted JListby MageLang Institute[Magercise | API Docs | Short Course| Magercises]Help is available for each task.
Task 1Either 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 2Create an instance variable of type SortedSet. Then, in the constructor create an instance of type TreeSet and assign it to the variable. Task 3At 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 4Besides 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 5Two 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 6In 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 7Compile 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. |