Creating Scrolling Lists in an Applet Window using Advanced Java Programming

Home  »  Java  »  Advance Java »    Java AWT (Abstract Window ToolKit) »    Scrolling List

Scrolling lists are similar to Choice menus or Choice lists with two significant differences:

  1. A scrolling list can be set up so that more than one item can be selected from the list at a time.
  2. Scrolling list do not pop-up when selected. Instead, multiple items are displayed in a manner similar to a text area. If the list contains more items they can be displayed, the scroll-bar is used to move through the entire list.

Scrolling lists are created from the List class. This is done by creating a List object and then adding individual items to the list. The list class has the following constructors:
  1. List () -- In this it create a scrolling list that enable us to select only one item at a time.
  2. List(int, Boolean) -- In this it specify first Argument how many items to be displayed on the list and then set whether the multiple items are selected or not for ex:-

List l=new List(3, True);
l.add("Red");
l.add("Green");
l.add("Yellow");
l.add("Blue");
add.(l);

The following example creates a list having list items.:


The above code will be stored in the file called ListTest.java, which on compilation will crate a class file called ListTest.class. Now compiple this file using below commands in the DOS mode:

C:\>jdk1.4\bin>javac ListTest.java
C:\>jdk1.4\bin>appletviewer ListTest.java


"Output of ListTest.java"


Download Complete Program

  -  



The above code creates an object of the List class by passing two arguments to the constructor of the List class. Number of items to be displayed and a boolean value. The boolean value indicates whether multiple items can be selected from the list or not.

Each item is added to the list using addItem() method.

Methods associated List class.

Method
Action
getSelectedIndex()this will gives us the index on the Selected item. And When a user will Select a item from a List this Method will gives the Index of the Selected Item.
getSelectedItems()this will gives us the Name or Text o Selected Item.
getItem(int)This will gives us the item of the Specified Number from the List .
countItems()This will gives us a integer which Specify the Total number of items in the list.
select(int)This will Select an item from a List , of a Specified Number.



AWT Controls



Label Buttons Check Boxes Radio Buttons
Choice Controls TextFields & Password TextAreas Scrolling Lists Scrollbars




No comments:

Post a Comment