List Example - Java AWT Example - Internet & Web Programming Lab

Program:


import java.awt.*;
import java.awt.event.*;

class ex4 extends Frame implements ActionListener,FocusListener
{
 List l1,l2;
 Label lb;
 Button b1,b2,b3,b4;
 ex4()
 {
   super("List example");
   setLayout(null);
   setSize(500,400);
   setVisible(true);
   l1=new List();
   l2=new List();
   lb=new Label();
   b1=new Button("<");
   b2=new Button(">");
   b3=new Button("<<");
   b4=new Button(">>");
        l1.setMultipleMode(true);
        l2.setMultipleMode(true);
        l1.add("Delhi");
        l1.add("Pune");
        l1.add("Mumbai");
        l1.add("Chennai");
   l1.setBounds(50,100,100,150);
   add(l1);
   l2.setBounds(300,100,100,150);
   add(l2);
   b1.setBounds(160,100,50,20);
   add(b1);
   b2.setBounds(160,130,50,20);
   add(b2);
   b3.setBounds(160,160,50,20);
   add(b3);
   b4.setBounds(160,190,50,20);
   add(b4);
   lb.setBounds(200,300,120,20);
   add(lb);
   b1.addActionListener(this);
   b2.addActionListener(this);
   b3.addActionListener(this);
   b4.addActionListener(this);
   b1.addFocusListener(this);
   b2.addFocusListener(this);
        Toolkit t=getToolkit();
        Dimension d=t.getScreenSize();
        int h=(int) d.getHeight();
        int w=(int) d.getWidth();
        setLocation(w/4, h/4);
   addWindowListener(new WindowAdapter()
   {
    public void windowClosing(WindowEvent we)
    {
     System.exit(0);
    }
   });
 }

 public void actionPerformed(ActionEvent ae)
 {
  int n;
  if(ae.getSource()==b1)
  {
   String s[]=l2.getSelectedItems();
   System.out.println(l2.getSelectedIndex());
   if(s.length>1)
    lb.setText("Please select only one item for this operation");
   else
   {
       l1.add(s[0]);
       l2.remove(s[0]);
   }
  }
  if(ae.getSource()==b2)
  {
   String s[]=l1.getSelectedItems();
   if(s.length>1)
     lb.setText("Please select only one item  for this operation");
   else
   {
     l2.add(s[0]);
     l1.remove(s[0]);
   }
  }
  if(ae.getSource()==b3)
  {
   String s[]= l2.getSelectedItems();
       for(int i=0;i<s.length;i++)
       {
           l1.add(s[i]);
           l2.remove(s[i]);
       }
  }
  if(ae.getSource()==b4)
  {
   String s[]= l1.getSelectedItems();
       for(int i=0;i<s.length;i++)
       {
           l2.add(s[i]);
           l1.remove(s[i]);
       }
  }
 }

 public void focusLost(FocusEvent fe)
 {
  if(fe.getSource()==b1)
    lb.setText(null);
  if(fe.getSource()==b2)
    lb.setText(null);
 }

 public void focusGained(FocusEvent fe)
 {}

 public static void main(String s[])
 {
  ex4 ob=new ex4();
 }
}

Output:

nn@linuxmint ~/Desktop/java 7 $ javac ex4.java
nn@linuxmint ~/Desktop/java 7 $ java ex4
nn@linuxmint ~/Desktop/java 7 $


1 comment:

Related Posts Plugin for WordPress, Blogger...