List Boxes

The List class allows the creation of a scrolling list of values that may be selected alone or together. If you want to create a list that does not allow multiple selections, use the following command line. Thelist is an instance of the list.

List thelist = new List();

If you want to create a list that does allow multiple selections, use the following command line which creates a list with 8 visible entries and multiple selections turned on. .

List thelist = new List(8, true);

Once you have created the list, the addItem method enables you to add new entries.

thelist.addItem("Coffee");
thelist.addItem("Bananas");
thelist.addItem("Oranges");

If you want to add an item at a specific location in the list, for example the first position, use the following line. Adding an item to position -1 or at a higher position than the number on the list, adds it to the end of the list.

thelist.addItem("Coffee", 0);

The following methods are also available for use with lists:

thelist.replaceItem("Milk", 0);

thelist.deleteItem(2);
thelist.deleteItems(5, 10);
thelist.clear();
int currentSelection = 
	theList.getSelectedIndex();
String selectItem =
         theList.getSelectedItem();
int currentSelections[];
currentSelections =
         theList.getSelectedIndexes();
String selectedItems[];
selectItems =
         theList.getSelectItems();
Replace the item in the first position with Milk

Delete the third item.

Delete items in positions 5 through 10 (The first position is 0).

Delete all of the items in the list.

This method returns the index of the currently selected item (not multiple selections) or -1 if no item is selected.

This method gets the item directly.

When multiple selections is used, this method returns the index of all the selections.



When multiple selections is used, this method returns all the selected items.

The List class does not use the action method. The handleEvent method must be used to catch list selection and deselection events. When an item on a list is selected, event.id is set equal to Event.LIST_SELECT. Also event.arg is an instance of an integer whose value is the index of the selected item. For the deselect event event.id will be set equal to Event.LIST_DESELECT. LIST_SELECT and LIST_DESELECT are declared in the Event class as static variables.

The example below creates two lists. One list is filled with items that may be selected for a shopping list while the other lists starts out empty. As you select items from the first list and click on the button with the right arrows, an event is triggered that causes each item to be deleted from the first list and added to the second list. The clear button will reset the lists to the original values.

 

The code for the Applet is listed below

import java.applet.*;
import java.awt.*;

public class shoplist extends Applet

{
  List slist = new List(8,false);
  List thelist = new List(10,false);
  public void init()
	{
	  fillthelist();
	  add(thelist);
	  add(new Button(">>>>"));
	  add(slist);
	  add(new Button("Clear"));
	  add(new Label("Select an item 
	    from the list on the left and
 	   hit >>>> to place it in the 
	    other list"));
	}
We import all the facilities of the AWT and applet that Java has to offer by using the * wildcard.

Create an applet named shoplist.


Create two lists :one allows 8 visible items before scrolling and the other allows 10. They do not allow multiple selections.

Call a user defined method to fill the list on the left. Add both lists and both buttons to the applet window with appropriate labels.


 public void fillthelist()
{
  thelist.addItem("Coffee");
  thelist.addItem("Bananas");
  thelist.addItem("Oranges");
  thelist.addItem("Pears");
  thelist.addItem("Peaches");
  thelist.addItem("Tuna");
  thelist.addItem("Bread");
  thelist.addItem("Milk");
  thelist.addItem("Eggs");
  thelist.addItem("Butter");
  thelist.addItem("Sugar");
  thelist.addItem("Cereal");
  thelist.addItem("Java Manual");
}
This user defined method fill thelist with 13 items that can be selected for a shopping list.


public boolean action(Event evt, 
  Object whatAction)
  {
    if(!(evt.target instanceof Button))
    {
      return false;
    } 
    String buttonLabel = (String) whatAction;
    if (buttonLabel == ">>>>")
      {
        slist.addItem(thelist.getSelectedItem());
        thelist.delItem(thelist.getSelectedIndex());
	return true;
      }
      else
	{
	  slist.clear();
	  thelist.clear();
	  fillthelist();
	  return true;
	}
}
}
When an event takes place the default handleEvent method calls the event method of our component. The whichAction parameter of the action method will be an instance of a Boolean class that is true if the item in the list was selected on and false if the item was unselected. The handleEvent method could be used to move the item to the other list immediately upon selection instead of using an additional button.

When the right arrows button is selected, the selected item is added to slist and deleted from thelist, else both lists are cleared and thelist is filled again.

The easiest way to obtain the code for this Applet is to click here and copy the contents of the screen that loads in your browser , paste it in a text editor and save it in a file called "shoplist.java".

After compiling this program we need to create an HTML page with a reference to the class file. It will require a graphic window that is large enough to contain the two lists, buttons, and label (width-501 and height-301).

Save the HTML code in the same directory as the class file.

When you load the HTML file in your browser the applet will execute as below.

Back to the Java AWT Components Page

Back to the Table of Contents