Event and Listener (Java Event Handling)

Event and Listener (Java Event Handling)

Changing the state of an object is known as an event. For example, click on button, dragging mouse etc. The java.awt.event package provides many event classes and Listener interfaces for event handling.

Java Event classes and Listener interfaces

Event Classes Listener Interfaces
ActionEvent ActionListener
MouseEvent MouseListener and MouseMotionListener
MouseWheelEvent MouseWheelListener
KeyEvent KeyListener
ItemEvent ItemListener
TextEvent TextListener
AdjustmentEvent AdjustmentListener
WindowEvent WindowListener
ComponentEvent ComponentListener
ContainerEvent ContainerListener
FocusEvent FocusListener

 

Steps to perform Event Handling

Following steps are required to perform event handling:

  1. 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){}

Java Event Handling Code

We can put the event handling code into one of the following places:

  1. Within class
  2. Other class
  3. Anonymous class

Java event handling by implementing ActionListener

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. class AEvent extends Frame implements ActionListener{
  4. TextField tf;
  5. AEvent(){
  6. //create components
  7. tf=new TextField();
  8. tf.setBounds(60,50,170,20);
  9. Button b=new Button(“click me”);
  10. b.setBounds(100,120,80,30);
  11. //register listener
  12. b.addActionListener(this);//passing current instance
  13. //add components and set size, layout and visibility
  14. add(b);add(tf);
  15. setSize(300,300);
  16. setLayout(null);
  17. setVisible(true);
  18. }
  19. public void actionPerformed(ActionEvent e){
  20. tf.setText(“Welcome”);
  21. }
  22. public static void main(String args[]){
  23. new AEvent();
  24. }
  25. }

public void setBounds(int xaxis, int yaxis, int width, int height); have been used in the above example that sets the position of the component it may be button, textfield etc.

event handling in java


2) Java event handling by outer class

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. class AEvent2 extends Frame{
  4. TextField tf;
  5. AEvent2(){
  6. //create components
  7. tf=new TextField();
  8. tf.setBounds(60,50,170,20);
  9. Button b=new Button(“click me”);
  10. b.setBounds(100,120,80,30);
  11. //register listener
  12. Outer o=new Outer(this);
  13. b.addActionListener(o);//passing outer class instance
  14. //add components and set size, layout and visibility
  15. add(b);add(tf);
  16. setSize(300,300);
  17. setLayout(null);
  18. setVisible(true);
  19. }
  20. public static void main(String args[]){
  21. new AEvent2();
  22. }
  23. }
  1. import java.awt.event.*;
  2. class Outer implements ActionListener{
  3. AEvent2 obj;
  4. Outer(AEvent2 obj){
  5. this.obj=obj;
  6. }
  7. public void actionPerformed(ActionEvent e){
  8. obj.tf.setText(“welcome”);
  9. }
  10. }

3) Java event handling by anonymous class

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. class AEvent3 extends Frame{
  4. TextField tf;
  5. AEvent3(){
  6. tf=new TextField();
  7. tf.setBounds(60,50,170,20);
  8. Button b=new Button(“click me”);
  9. b.setBounds(50,120,80,30);
  10. b.addActionListener(new ActionListener(){
  11. public void actionPerformed(){
  12. tf.setText(“hello”);
  13. }
  14. });
  15. add(b);add(tf);
  16. setSize(300,300);
  17. setLayout(null);
  18. setVisible(true);
  19. }
  20. public static void main(String args[]){
  21. new AEvent3();
  22. }
  23. }
java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Event and Listener (Java Event Handling) Changing the state of an object is known as an event. For example, click on button, dragging mouse etc. The java.awt.event package provides many event classes and Listener interfaces for event handling. Java Event classes and Listener interfaces Event Classes Listener Interfaces ActionEvent ActionListener MouseEvent MouseListener and MouseMotionListener MouseWheelEvent […]

Tags:

....
...