recent

Java Program to implement ListBox

Write a Java Program to implement ListBox:-

public class AWT1
{
public static void main(String args[])
{
MyFrame mf = new MyFrame();
}
}
import java.awt.*;
import java.awt.event.*;
public class MyFrame extends Frame implements ActionListener
{
List lst;
Button btn;
MyFrame()
{
super("Sample Java Frame");
addWindowListener(new MyWindowAdapter());
setSize(500,400);
addControls();
setVisible(true);
}
private void addControls()
{
setLayout(null);
lst = new List();
lst.setLocation(30,50);
lst.setSize(200,300);
lst.setMultipleMode(true);
add(lst);
lst.add("sfsdf");
lst.add("55656");
lst.add("dfgdfg");
lst.add("sfsdf");
lst.add("cvb");
lst.add("sfcvbcbcvbcvbsdf");
lst.add("bmmbnm");
lst.add("ioouo");
lst.add("qeqwe");
lst.add(".m,.m,.");
btn = new Button("Click Me");
btn.addActionListener(this);
btn.setSize(100,24);
btn.setLocation(250,50);
add(btn);
}
public void actionPerformed(ActionEvent ae)
{
System.out.println("Total Selected Items : " +
lst.getSelectedItems().length);
String aItem[] = lst.getSelectedItems();
int i;
for (i=0;i<aItem.length;i++)
System.out.println(aItem[i]);
}
}
import java.awt.event.*;
class MyWindowAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
Powered by Blogger.