Choice class with button and label
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet
code="ListBox" height=200 width=200>
</applet>
*/
public class ListBox extends
Applet implements ActionListener
{
Button
b1,b2,b3;
TextField
tf;
List
lst;
public
void init()
{
Label
l1=new Label("List Box
:");
add(l1);
lst=new List(5);
add(lst);
Label
l2=new Label("Enter the Text :");
add(l2);
tf=new
TextField(20);
add(tf);
b1=new
Button("Add");
add(b1);
b1.addActionListener(this);
b2=new
Button("Remove");
add(b2);
b2.addActionListener(this);
b3=new
Button("Clear All");
add(b3);
b3.addActionListener(this);
}
public
void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
/*
using the add button */
String
s=tf.getText();
lst.add(s);
}
else
{
if(ae.getSource()==b2)
{
/*
using the remove button */
if(lst.getSelectedIndex()!=-1)
lst.remove(lst.getSelectedIndex());
}
else
{
if(ae.getSource()==b3)
{
/*using
the clear all button */
lst.removeAll();
}
}
}
}
}
Comments
Post a Comment