• Changing the state of an object is known as an event.


  • Steps to perform Event Handling Following steps are required to perform event handling:


  • Register the component with the Listener


  • Registration Methods For registering the component with the Listener, many classes provide the registration methods. For example:


  • Button


    • public void addActionListener(ActionListener a){}


  • MenuItem


    • public void addActionListener(ActionListener a){}


  • TextField


    • public void addActionListener(ActionListener a){}


    • public void addTextListener(TextListener a){}


  • TextArea


    • public void addTextListener(TextListener a){}


  • Checkbox


    • public void addItemListener(ItemListener a){}


  • Choice


    • public void addItemListener(ItemListener a){}


  • List


    • public void addActionListener(ActionListener a){}


    • public void addItemListener(ItemListener a){}


     
    import java.awt.*;  
    import java.awt.event.*;  
    class AEvent extends Frame implements ActionListener{  
    TextField tf;  
    AEvent(){  
      
    //create components  
    tf=new TextField();  
    tf.setBounds(60,50,170,20);  
    Button b=new Button("click me");  
    b.setBounds(100,120,80,30);  
      
    //register listener  
    b.addActionListener(this);//passing current instance  
      
    //add components and set size, layout and visibility  
    add(b);add(tf);  
    setSize(300,300);  
    setLayout(null);  
    setVisible(true);  
    }  
    public void actionPerformed(ActionEvent e){  
    tf.setText("Welcome");  
    }  
    public static void main(String args[]){  
    new AEvent();  
    }  
    }  
    
    
    
     
    import java.awt.*;  
    import java.awt.event.*;  
    class AEvent2 extends Frame{  
    TextField tf;  
    AEvent2(){  
    //create components  
    tf=new TextField();  
    tf.setBounds(60,50,170,20);  
    Button b=new Button("click me");  
    b.setBounds(100,120,80,30);  
    //register listener  
    Outer o=new Outer(this);  
    b.addActionListener(o);//passing outer class instance  
    //add components and set size, layout and visibility  
    add(b);add(tf);  
    setSize(300,300);  
    setLayout(null);  
    setVisible(true);  
    }  
    public static void main(String args[]){  
    new AEvent2();  
    }  
    }  
    import java.awt.event.*;  
    class Outer implements ActionListener{  
    AEvent2 obj;  
    Outer(AEvent2 obj){  
    this.obj=obj;  
    }  
    public void actionPerformed(ActionEvent e){  
    obj.tf.setText("welcome");  
    }  
    }  
    
    
     
    import java.awt.*;  
    import java.awt.event.*;  
    class AEvent3 extends Frame{  
    TextField tf;  
    AEvent3(){  
    tf=new TextField();  
    tf.setBounds(60,50,170,20);  
    Button b=new Button("click me");  
    b.setBounds(50,120,80,30);  
      
    b.addActionListener(new ActionListener(){  
    public void actionPerformed(){  
    tf.setText("hello");  
    }  
    });  
    add(b);add(tf);  
    setSize(300,300);  
    setLayout(null);  
    setVisible(true);  
    }  
    public static void main(String args[]){  
    new AEvent3();  
    }  
    }
    
    
  • The button class is used to create a labeled button that has platform independent implementation.


  •  
    import java.awt.*;  
    public class ButtonExample {  
    public static void main(String[] args) {  
        Frame f=new Frame("Button Example");  
        Button b=new Button("Click Here");  
        b.setBounds(50,100,80,30);  
        f.add(b);  
        f.setSize(400,400);  
        f.setLayout(null);  
        f.setVisible(true);   
    }  
    }  
    
    
  • Java AWT Button Example with ActionListener


  •  
    import java.awt.*;  
    import java.awt.event.*;  
    public class ButtonExample {  
    public static void main(String[] args) {  
        Frame f=new Frame("Button Example");  
        final TextField tf=new TextField();  
        tf.setBounds(50,50, 150,20);  
        Button b=new Button("Click Here");  
        b.setBounds(50,100,60,30);  
        b.addActionListener(new ActionListener(){  
        public void actionPerformed(ActionEvent e){  
                tf.setText("Welcome to Javatpoint.");  
            }  
        });  
        f.add(b);f.add(tf);  
        f.setSize(400,400);  
        f.setLayout(null);  
        f.setVisible(true);   
    }  
    }