Wednesday, 5 March 2014

AWT CONTROLS:Lists control

The List represents a list of text items............ 

using lists


         The List represents a list of text items. The list can be configured to that user can choose either one item or multiple items.The List provides a compact,multiple-choice ,scrolling selection list.Unlike the choice object,which shows only the single selected item in the menu,aList object can be constructed to show any number of choices in the visible window.Itcan also be constructed to allow multiple selections.

List provides these constructors:


  •  List()

              Creates a new scrolling list. Initially there are no visible lines, and only one item can be selected from the list. 

  •  List(int numRows)

             Creates a new scrolling list initialized with the specified number of visible lines. By default, multiple selections are not allowed.Parameter,rows specified by the number of items to show.

  •  List(int numRows,boolean multipleMode)

             Creates a new scrolling list initialized to display the specified number of rows. If the value of multipleMode is true, then the user can select multiple items from the list. If it is false, only one item at a time can be selected.Parameters,numRows specified by the number of items to show and multipleMode,if true, then multiple selections are allowed; otherwise, only one item can be selected at a time .

Lists provides these methods:


  • add(String item) :Adds the specified item to the end of scrolling list.

           syntax:
                void add(String item) 

  • add(String item, int index) :Adds the specified item to the the scrolling list at the position indicated by the index

          syntax:
         void add(String item, int index) 

  • getSelectedIndex():Gets the index of the selected item on the list,

          syntax
                int getSelectedIndex() 

  • getSelectedItem() :Gets the selected item on this scrolling list.

         syntax:
       String getSelectedItem() 

  • int[] getSelectedIndexes() :Gets the selected indexes on the list.It return an array containing the indexes of the currentily selected items.

            syntax:
                  int[] getSelectedIndexes() 

  • String[] getSelectedItems() :Gets the selected items on this scrolling list.

            syntax:
                  String[] getSelectedItems()

  • void select(int index) :Selects the item at the specified index in the scrolling list

             syntax:
                 void select(int index)


simple program for handling lists


import java.applet.Applet;
import java.awt.Graphics;
import java.awt.List;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener; 
/*<applet code="GetSelectedItemFromMultiSelectExample" width=200 height=200></applet>*/ 
public class GetSelectedItemFromMultiSelectExample extends Applet implements ItemListener
{
List list = null;   
public void init()
{               
list = new List(5, true);    //create a multi select list
 //add items to a list  
list.add("One");
list.add("Two");
list.add("Three");
list.add("Four");
list.add("Five");
list.add("Six");
list.add("Seven");               
add(list);   //add list
list.addItemListener(this); //add listener               
}
public void paint(Graphics g)
{
String[] items = list.getSelectedItems();
String msg = "";                
for(int i=0; i < items.length; i++)
{               
msg = items[i] + " " + msg;
}
g.drawString("Selected Items: "+ msg, 10, 120);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();     

}

output of this program is:



No comments:

Post a Comment