Event handling within the class using all AWT Components in Java

Delegation Event Model :

A mechanism for controlling the events and deciding what should happen after an event occur is refferred to as event handling.

The delegation Event Model consists of source and Listener.

Examples of Event Listeners:  

1] ActionListener
2] AdjustmentListener
3] ComponentListener 
4] ContainerListener
5] MouseLIstener

AWT components used:

1] Frame
2] Label
3] TextField
4] TextArea
5] Checkbox
6] CheckboxGroup
7] Choice
8] List 
9] Button


Code: 


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

class P extends Frame implements ActionListener {

    TextField tf1;
    Label lb1, l1, lb3, l2, l3, lb4, l4, lb5, l5, lb6, l6;
    Checkbox chk1, chk2, chk3, chk4, chk5;
    CheckboxGroup chk_obj;
    Choice c1;
    List ls1;

    P()
    {   
        lb1 = new Label("Name :");
        lb1.setBounds(50,50,50,30); 
        add(lb1);
        tf1 = new TextField("");
        tf1.setBounds(200, 50, 200, 20);
        add(tf1);
        l1 = new Label("");
        l1.setBounds(50,440,200,30); 
        add(l1);

        lb3 = new Label("Years Completed :");
        lb3.setBounds(50,90,130,30); 
        add(lb3);
        chk1 = new Checkbox("1st", true);
        chk1.setBounds(200, 90, 60, 30);
        add(chk1);
        chk2 = new Checkbox("2nd");
        chk2.setBounds(260, 90, 60, 30);
        add(chk2);
        l2 = new Label("");
        l2.setBounds(50,460,200,30);
        add(l2);
        l3 = new Label("");
        l3.setBounds(50,480,200,30);
        add(l3);

        Label lb4 = new Label("Gender :");
        lb4.setBounds(50,120,50,30); 
        add(lb4);
        chk_obj = new CheckboxGroup();
        chk3 = new Checkbox("Male", chk_obj ,true);
        chk3.setBounds(200, 120, 50, 30);
        add(chk3);
        chk4 = new Checkbox("Female", chk_obj, false);
        chk4.setBounds(260, 120, 70, 30);
        add(chk4);
        chk5 = new Checkbox("Others", chk_obj, false);
        chk5.setBounds(330, 120, 60, 30);
        add(chk5);
        l4 = new Label("");
        l4.setBounds(50,500,200,30);
        add(l4);

        lb5 = new Label("Languages :");
        lb5.setBounds(50,160,70,30); 
        add(lb5);
        c1 = new Choice();
        c1.setBounds(200, 160, 80, 40);
        c1.add("Java");
        c1.add("C");
        c1.add("C++");
        c1.add("PHP");
        c1.add("Python");
        add(c1);
        l5 = new Label("");
        l5.setBounds(50,520,200,30);
        add(l5);

        lb6 = new Label("Branch :");
        lb6.setBounds(50,210,70,30); 
        add(lb6);
        ls1 = new List();
        ls1.setBounds(200, 210, 120, 50);
        ls1.add("Information Tech.");
        ls1.add("Computer");
        ls1.add("Mech.");
        ls1.add("Meta.");
        add(ls1);
        l6 = new Label("");
        l6.setBounds(50,540,200,30);
        add(l6);
        
        Button button = new Button("submit");
        button.setBounds(140,290,50,30);
        add(button);
        button.addActionListener(this);
        

        setSize(700,700); 
        setLayout(null);
        setVisible(true); 
    }

    // implementing method of actionListener
    public void actionPerformed(ActionEvent e)
    {
        String s1 = tf1.getText();
        l1.setText("Your name is : " + s1);

        boolean b1 = chk1.getState();
        l2.setText("Male : " + b1);        
        boolean b2 = chk2.getState();
        l3.setText("Female : " + b2);

        String s2 = chk_obj.getSelectedCheckbox().getLabel();
        l4.setText("Year : " + s2);

        String s3 = c1.getSelectedItem();
        l5.setText("Languages : " + s3);

        // String s4[] = ls1.getSelectedItems();
        String s4 = ls1.getSelectedItem();
        l6.setText("Branch : " + s4);
    }

    public static void main(String[] args)
    {
    new P();
    }
}


Output:




Comments