Listener

1.    An Event is an object generated by the component on user inter action
2.    An Event Source is a component which generated the Event.
3.    An Event Listener is an object to which the component has delegated the task of handling the Event.
4.    The Event Handlers are the methods inside the Event Listener which actually handle the Event.
5.    The Event Listener should be registered with the Event Source to receive Events.

Application 1 :     Take a Login and Password from the user and display it on the screen on clicking OK button and clear both the TextFields on clicking RESET button.








import java.awt.event.*;
import java.awt.*;
import java.applet.*;
class Login1 implements ActionListener
{
    Frame f;
    Label l1, l2;
    TextField tf1, tf2,tf3;
    Button b1, b2;

    Login1()
    {
        f=new Frame("Login");
        l1=new Label("Login");
        l2=new Label("Password");
        tf1=new TextField(10);
        tf2=new TextField(10);
        tf3 =new TextField(35);
        b1=new Button("OK");
        b2=new Button("RESET");

        //Change in Layout Manager if necessary
        f.setLayout(new FlowLayout());

        f.add(l1);
        f.add(tf1);
        f.add(l2);
        f.add(tf2);
        f.add(b1);
        f.add(b2);
        b1.addActionListener(this);
        b2.addActionListener(this);
        tf2.setEchoChar('*');

        f.setSize(100,400);
        f.setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
       {
        if (e.getSource() == b1)
        {
            String s = tf1.getText();
            String s1 = tf2.getText();
            System.out.println("Login = "+s+" "+"Password = "+s1);
        }
        else
        {
            tf1.setText("  ");
            tf2.setText("");
        }
    }
    public static void main(String [] args)
    {
        Login1 x = new Login1();
    }
}

Applet 1 : Take a Login and Password from the user and display it on the third TextField which appears only on clicking OK button and clear both the TextFields on clicking RESET button.







import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class LoginApplet1 extends Applet implements ActionListener
{
    Frame f;
    Label l1, l2;
    TextField tf1, tf2, tf3;
    Button b1,b2;

    public void init()
    {
        f=new Frame("Login");
        l1=new Label("Login");
        l2=new Label("Password");
        tf1=new TextField(10);
        tf2=new TextField(10);
        tf3=new TextField(35);
        b1=new Button("OK");
        b2=new Button("RESET");
        tf3.setVisible(false);
       
        add(l1);
        add(tf1);
        add(l2);
        add(tf2);
        add(b1);
        add(b2);
        add(tf3);

        b1.addActionListener(this);
        b2.addActionListener(this);
        tf2.setEchoChar('*');

    }

    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == b1)
        {
            String s = tf1.getText();
            String s1 = tf2.getText();
            tf3.setText("Login : "+s+" "+"Password : "+s1);
            tf3.setVisible(true);
        }
        else
        {
            tf3.setVisible(false);
            tf1.setText(" ");
            tf2.setText("");
        }
        validate();  // to rearrange controls
        repaint();   // to get back to original screen
    }
}

Click new -> File -> html    and save this file as LoginApplet1.html

<BODY>

<Applet code=LoginApplet1.class height=100 width=400 >

</Applet>

</BODY>


Application 2 :  Display 4 Buttons (“NORTH”, “SOUTH”, “EAST”,”WEST”) in four parts of Frame and TextArea in the center. On clicking any button, display the button name in the TextArea.

 


import java.applet.*;
import java.awt.*;
import java.awt.event.*;
class Frame1 implements ActionListener
{
    Frame f;
    TextArea ta;
    Button b1, b2, b3, b4;

    Frame1()
    {
        f = new Frame("Border Frame");
        ta = new TextArea(20,20);
        b1 = new Button("NORTH");
        b2 = new Button("SOUTH");
        b3 = new Button("EAST");
        b4 = new Button("WEST");

        f.add(b1,BorderLayout.NORTH);
        f.add(b2,BorderLayout.SOUTH);
        f.add(b3,BorderLayout.EAST);
        f.add(b4,BorderLayout.WEST);
        f.add(ta,BorderLayout.CENTER);

        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);

        f.setSize(400,400);
        f.setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == b1)
            ta.append("NORTH\n");
        else
            if(e.getSource() == b2)
                ta.append("SOUTH\n");
            else
                if(e.getSource() == b3)
                    ta.append("EAST\n");
                else
                    ta.append("NORTH\n");
    }
    public static void main(String [] args)
    {
        Frame1 f = new Frame1();
    }
}

Applet 2 : Same as above
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class FrameApplet1 extends Applet implements ActionListener
{
    Frame f;
    TextArea ta;
    Button b1, b2, b3, b4;

    public void init()
    {
        f = new Frame("Border Frame");
        ta = new TextArea(20,20);
        b1 = new Button("NORTH");
        b2 = new Button("SOUTH");
        b3 = new Button("EAST");
        b4 = new Button("WEST");

        add(b1,BorderLayout.NORTH);
        add(b2,BorderLayout.SOUTH);
        add(b3,BorderLayout.EAST);
        add(b4,BorderLayout.WEST);
        add(ta,BorderLayout.CENTER);

        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == b1)
            ta.append("NORTH\n");
        else
            if(e.getSource() == b2)
                ta.append("SOUTH\n");
            else
                if(e.getSource() == b3)
                    ta.append("EAST\n");
                else
                    ta.append("NORTH\n");
        validate();
        repaint();
    }
}

Click new -> File -> html    and save this file as FrameApplet1.html

<BODY>

<Applet code=FrameApplet1.class height=100 width=400 >

</Applet>

</BODY>


Rules of AWT :

1) Declare the Controls.
2) Initialize the Controls in Constructor or init() method.
3) Change Layout Manager.
4) Add Controls to Container.
5) Register the Listener.
6) Set Size and Visibility of the Frame.

Frame is for grouping Controls.
Panel is for grouping Components.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class FramePanel extends Applet implements ActionListener
{
    Frame f;
    TextField tf1;
    Button b1;
    Panel p;
    TextArea ta1;

    public void init()
{
        f = new Frame("Frame With Panel");
        tf1 = new TextField(20);
        b1 = new Button("OK");
        p = new Panel();
        ta1 = new TextArea(10,20);
        add(ta1);
        add(p);
        p.add(tf1);
        p.add(b1);
        add(p,BorderLayout.SOUTH);
        tf1.requestFocus();
        tf1.addActionListener(this);
        b1.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e)
    {
        String s = tf1.getText();
        tf1.setText(" ");
        tf1.requestFocus();
        ta1.append(s+"\n");
    }
}

Click new -> File -> html    and save this file as FrameApplet1.html

<BODY>

<Applet code=FramePanel1.class height=100 width=400 >

</Applet>

</BODY>

Applet : Whatever is typed inside TextField is added to dropdown Choice Popup when ADD Button is selected and is deleted from dropdown Choice Popup when DELETE Button is clicked.

 
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class FramePopup extends Applet implements ActionListener
{
    Frame f;
    TextField tf1;
    Button b1,b2;
    Choice c;
    public void init()
{
        f = new Frame("Frame With Popup");
        tf1 = new TextField(20);
        b1 = new Button("ADD");
        b2 = new Button("DELETE");
        c = new Choice();
        add(tf1);
        add(b1);
        add(b2);
        add(c);
        b1.addActionListener(this);
        b2.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == b1)
        {
            String s = tf1.getText();
            c.add(s);
            tf1.setText(" ");
        }
        else if(e.getSource() == b2)
        {
            String s1 = c.getSelectedItem();
            c.remove(s1);
            tf1.setText(" ");
        }
        f.validate();
        f.repaint();
       }
}

Click new &#61664; File &#61664; html    and save this file as FramePopup.html

<BODY>

<Applet code=FramePopup.class height=100 width=400 ></Applet>

</BODY>


Application : There are two Panels with different background. In one panel, there are two Buttons. On clicking ADD Button, a TextField appears. Whatever user type in this TextField is added as a Button in second Panel and at the same time the TextField disappears. On clicking DELETE Button, again a TextField appears. After user type in this TextField, the same name Button gets deleted from second Panel.
 
 import java.awt.*;
import java.applet.*;
import java.awt.event.*;
class FramePanel2 implements ActionListener
{
    Frame f;
    Panel p1, p2;
    TextField tf1, tf2;
    Button b1,b2;

    FramePanel2()
    {
        f=new Frame("Frame with Panels");
        p1 = new Panel();
        p1.setBackground(Color.red);
        p2 = new Panel();
        p2.setBackground(Color.green);
        b1 = new Button("ADD");
        b2 = new Button("DELETE");

        //Change in Layout Manager if necessary
        f.setLayout(new FlowLayout());

        f.add(p1);
        p1.add(b1);
        p1.add(b2);
        f.add(p2);

        b1.addActionListener(this);
        b2.addActionListener(this);

        f.setSize(400,400);
        f.setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
{
        if (e.getSource() == b1)
        {
            tf1 = new TextField(20);
            tf1.addActionListener(this);
            p1.add(tf1);
            tf1.requestFocus();
            b1.setEnabled(false);
            b2.setEnabled(false);
        }
        else
        if(e.getSource() == tf1)
        {
            String s1 = tf1.getText();
            tf1.setText(" ");
            p1.remove(tf1);
            Button b3 = new Button(s1);
            p2.add(b3);
            b1.setEnabled(true);
            b2.setEnabled(true);
        }
        else
        if(e.getSource() == b2)
        {
            tf2 = new TextField(20);
            tf2.addActionListener(this);
            p1.add(tf2);
            tf2.requestFocus();
            b1.setEnabled(false);
            b2.setEnabled(false);
        }
        else
        if(e.getSource() == tf2)
        {
            String s2 = tf2.getText();
            Component c[] = p2.getComponents();
            for(int i = 0; i < c.length; i++)
            {
                if(c[i] instanceof java.awt.Button)
                {
                    Button b4 = (Button) c[i];
                    if(s2.equals(b4.getLabel()))
                    {
                        p2.remove(b4);
                    }
                }
            }
            p1.remove(tf2);
            b1.setEnabled(true);
            b2.setEnabled(true);
          
            Component c1[] = p2.getComponents();
            if(c1.length == 0)
            {
                b2.setEnabled(false);
            }
        }
        f.validate();
        f.repaint();
    }
    public static void main(String [] args)
    {
        FramePanel2 x = new FramePanel2();
    }
}

Application : Menu “File” is displayed on the Frame. On clicking File the Popup containing “Open”, “Save” and “Exit”; options are displayed. On clicking “Open” option FileDialog appears which let user to open file from hard disk. On clicking “Save” option FileDialog appears which let user to save file to hard disk. On clicking “Exit” option Panel containing “YES” and “NO” Buttons appear. On clicking “YES” Button application is closed whereas on clicking “NO” Button, Panel disappear.


import java.awt.*;
import java.awt.event.*;
class FrameMenu implements ActionListener
{
    Frame f;
    Menu m;
    MenuBar mb;
    MenuItem m1, m2, m3;
    Panel p;
    Button b1, b2;
    Dialog d;
    FileDialog fd1, fd2;

    FrameMenu()
    {
        f=new Frame("Frame with Menu");
        m = new Menu("File");
        mb = new MenuBar();
        m1 = new MenuItem("Open");
        m2 = new MenuItem("Save");
        m3 = new MenuItem("Exit");
        fd1 = new FileDialog(f, "Open File",FileDialog.LOAD);
        fd2= new FileDialog(f, "Save File",FileDialog.SAVE);
        d = new Dialog(f, "Are You Sure");
        p = new Panel();
        b1 = new Button("YES");
        b2 = new Button("NO");

        //Change in Layout Manager if necessary
        //f.setLayout(new FlowLayout());

        m.add(m1);
        m.add(m2);
        m.add(m3);
        mb.add(m);
        f.setMenuBar(mb);

        m1.addActionListener(this);
        m2.addActionListener(this);
        m3.addActionListener(this);

        f.setSize(200,200);
        f.setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
       {
        if (e.getSource() == m1)
        {
            fd1.show();
        }
        else
        if(e.getSource() == m2)
        {
            fd2.show();
        }
        else
        if(e.getSource() == m3)
        {
            d.setSize(100,100);
            d.setVisible(true);
            d.add(p);
            p.add(b1);
            p.add(b2);
            b1.addActionListener(this);
            b2.addActionListener(this);
        }
        else
        if(e.getSource() == b1)
        {
            System.exit(0);
        }
        else
        if(e.getSource() == b2)
        {
            d.setVisible(false);
        }
        f.validate();
        f.repaint();
    }
    public static void main(String [] args)
    {
        FrameMenu x = new FrameMenu();
    }
}