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

[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.


[ 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.