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 3. How to use an ArrayList with a JComboBox page 6 of 9


If you've ever looked at the data model class for the JComboBox component of the JFC/Swing component set, you may have noticed that the data model is backed by a Vector. If, however, you don't need the synchronized access of a Vector (thus increasing performance) or you prefer the new Collections Framework, you can create your own implementation of the ComboBoxModel or MutableComboBoxModel interface for storing the data in a List or more specifically in a ArrayList.

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.

Skeleton Code

Task 1:
Either start with the skeleton code or create an ArrayListComboBoxModel class. The class extends AbstractListModel and implements MutableComboBoxModel.

Help for task 1:
Shift click to save the file to your working directory.

Task 2:
Create a variable of type List. Refer the List argument in the constructor to your variable.

Task 3:
The AbstractListModel class leaves 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 list saved in the prior step.

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

Use the get(int position) method of List to complete getElementAt().

Task 4:
By stating that the ArrayListComboBoxModel class implements the MutableComboBoxModel interface, you are saying you'll provide implementations for the methods of both the MutableComboBoxModel and ComboBoxModel interfaces, as the former extends the latter. The getSelectedItem() and setSelectedItem() methods of the ComboBoxModel interface are already defined for you.

Task 5:
The MutableComboBoxModel interface, defines four methods: addElement(Object element), insertElementAt(Object element, int position), removeElement(Object element), and removeElementAt(int position). Complete the stubs such that they alter the list saved in a prior step.

Help for task 5:
Use the add(Object element) method of List to insert an element at the end.

Use the add(Object element, int position) method of List to insert an element at a designated position.

Use the remove(Object element) method of List to remove the first instance of an element.

Use the remove(int position) method of List to remove an element at a designated position.

Task 6:
Compile your program and run the Tester program to see what happens. Provide several names as command line arguments. The Tester program tests your new data model class by adding and removing elements from the model.

Help for task 6:



java Tester Jim Joe Mary

Check your output to make sure that Jim, Joe, and Mary are added to the names you provided.


Main menuSection menuGive feedback on this tutorialPreviousNext
PrivacyLegalContact