List control in Java awt:
The list class can be used to add a list of items with several visible rows. The list represents a list of text items.List control is similar to choice control.The constructor in java awt list:
The list class has three constructors.T hey are :List()
It is used to create an empty list with the default four rows visible.
List(int rowsize)
It is used to create an empty list with the specified number of visible rows.
List(int rowsize,Boolean multiselect )
It is used to create an empty list with the specified number of visible rows and the specified multiple selections. If multi-select is true, a user may select multiple entries in a list. if multiple select is false, only one item can be selected.
<
Methods available in Java AWT list control :
The most commonly used list() methods are as follows:
void addItemListener(ItemListener listener)
This method is used to specify an event handler for the list.
void add(String str)
This method is used to add string into the list at the next position.
void add(String str,int index)
This method is used to add the string into the list at the specified position.
int getItemCount()
This method is used to return the total number of items.
int getSelectedItem()
This method is used to return the index of the selected item.
void select(int i)
This method is used to set the index of the selected item
void remove(int i)
This method is used to remove at the index i.
void removeAll()
This method is used to remove items.
Sample Program:
import java.awt.*;import java.applet.*;
import java.awt.event.*;
public class listdemo extends Applet implements ActionListener,ItemListener
{
TextArea t1;
public void init()
{
List lst=new List();
lst.add(“programming languages”);
lst.add(“ C programming”);
lst.add(“C++ programming”);
lst.add(“Java programming”);
lst.addActionListener(this);;
lst.addItemListener(this);
add(lst);
t1=new TextArea(15,25)
add(t1);
}
public void actionPerformed(ActionEvent ae)
{
t1.append(“Action Event:”+ae.getActionCommand()+”\n”);
}
public void itemStateChanged(ItemEvent ie)
{
List list=(List) ie.getItemSelectable();
t1.append(“Item Event:”+list.getSelectedItem()+”\n”);
}
}
No comments:
Post a Comment