Java's Advanced User Interface Components - More on the List Class
(Page 2 of 4 )
The following example is a simple applet that creates a scrolling list and populates it with items, the list is added to the container in this case Applet using the add() method. In the example below we have a list of seven dairy items with multi-select option set to true.
/* <Applet code “ScrollListTest.class”
Width = 200
Height = 200>
</applet>
*/
Import java.awt.*;
Public class ScrollListTest extends java.applet.Applet
{
List Dairy = new List (7,true)
Public void init ()
{
Diary.addItem (“Whole Milk”);
Diary.addItem (“Skimmed Milk”);
Diary.addItem (“Butter”);
Diary.addItem (“Cheese”);
Diary.addItem (“Yogurt”);
Diary.addItem (“Ice Cream”);
Diary.addItem (“ Butter Milk ”);
}
}
Scrolling lists generate actions when the user double-clicks a list item, for example a single mouse click generates a
LIST_SELECT or
LIST_DESELECT event ID. A scrolling list action has the argument of the string of the item that was double-clicked. The following table shows some of the methods available to scrolling lists. See the API documentation for a complete set.
| Method | Action |
|---|
getItem(int)
| Returns the string item at the given position (items inside a list begin at 0, just like arrays)
|
countItems()
| Returns the number of items in the menu
|
getSelectedIndex()
| Returns the index position of the item that's selected
|
getSelectedIndexes()
| Returns an array of index positions (used for lists that allow multiple selections)
|
getSelectedItem()
| Returns the currently selected item as a string
|
getSelectedItems()
| Returns an array of strings containing all the selected items
|
select(int)
| Selects the item at the given position
|
select(String)
| Selects the item with the given string
|
The following example creates a slightly complex Applet which contains the following UI components: A TextField to accept user input; a Label, to inform the user of the contents that is to be entered in the TextField; a List, which displays a number of items; a “but_add” button, which let’s the user to add text in the TextField to the List. When the applet is started, the UI components specified are added to the applet. The addItem() method is used to add the items to the list. The text entered in the TextField is added to the List on clicking the add button. The ActionListener() method enables the program to listen to the button click events. The items are added to the list inside the actionPerformed() method.
/*
&lgt;applet code = "ListTest1.class" height = 300 width = 200>
&lgt;/applet>
*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
Public class ListTest1 extends Applet
{
List acts = new List();
TextField tx = new TextField(10);
Button but_add = new Button("ADD");
String stringlist[] = {"One", "Two", "Three", “Four”};
Public void start ()
{
add(new Label("Text"));
add(tx);
for(int i = 0; i < stringlist.length; ++i)
acts.addItem(stringlist[i]);
add(acts);
but_add.addActionListener(new AddLisn());
add(but_add);
}
Class AddLisn implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
acts.addItem(tx.getText());
}
}
}
Next: Scrollbars and Sliders >>
More Java Articles
More By Gayathri Gokul