Sun Java Solaris Communities My SDN Account Join SDN
 
Tutorials & Code Camps

Help Using an ArrayList with a JComboBox

 


[Magercise | API Docs | Short Course| Magercises]

Help is available for each task.

Task 1

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

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.

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.

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.

java Tester Jim Joe Mary

Check your output to make sure that Dick and Harry are added to the names you provided.

Copyright © 1999 MageLang Institute. All Rights Reserved.