Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Online Training

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
 

Using an ArrayList with a JComboBox


by MageLang Institute

[Help | API Docs | Short Course| Magercises]

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 MutableCom boBoxModel interface for storing the data in a List or more specifically in a ArrayList.

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.

Skeleton Code

Tasks


  1. Either start with the skeleton code or create an ArrayListComboBoxModel class. The class extends AbstractListM odel and implements MutableCom boBoxModel.

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

  3. The AbstractListM odel class leaves the getSize() and get ElementAt() 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.

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

  5. The MutableCom boBoxModel 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.

  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.

Where help exists, the task numbers above are linked to the step-by-step help page.

Solution Source

Demonstration

When you run the Tester program, from the command line, with an argument list of Jim Joe Mary, you'll see the following screen after opening up the list of selections for the JComboBox:

Your output will differ if you provide different command line options (or your implementation is wrong). The Tester program automatically adds Tom, Dick, Harry, and Bill then deletes Tom and Bill. This tests your class.

Next Magercise Magercises Short Course

Copyright © 1999 MageLang Institute. All Rights Reserved.


[ This page was updated: 29-Sep-99 ]

Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-99 Sun Microsystems, Inc.
All Rights Reserved. Legal Terms. Privacy Policy.