Event Handling

PREVIOUS

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.

Event classes and Listener interfaces:

Event ClassesListener Interfaces
ActionEventActionListener
MouseEventMouseListener and MouseMotionListener
MouseWheelEventMouseWheelListener
KeyEventKeyListener
ItemEventItemListener
TextEventTextListener
AdjustmentEventAdjustmentListener
WindowEventWindowListener
ComponentEventComponentListener
ContainerEventContainerListener
FocusEventFocusListener

Steps to perform Event Handling

Following steps are required to perform event handling:
  1. Implement the Listener interface and overrides its methods
  2. Register the component with the Listener

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

EventHandling Codes:

We can put the event handling code into one of the following places:
  1. Same class
  2. Other class
  3. Annonymous class

Example of event handling within class:

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3.   
  4. class AEvent extends Frame implements ActionListener{  
  5. TextField tf;  
  6. AEvent(){  
  7.   
  8. tf=new TextField();  
  9. tf.setBounds(60,50,170,20);  
  10.   
  11. Button b=new Button("click me");  
  12. b.setBounds(100,120,80,30);  
  13.   
  14. b.addActionListener(this);  
  15.   
  16. add(b);add(tf);  
  17.   
  18. setSize(300,300);  
  19. setLayout(null);  
  20. setVisible(true);  
  21.   
  22. }  
  23.   
  24. public void actionPerformed(ActionEvent e){  
  25. tf.setText("Welcome");  
  26. }  
  27.   
  28. public static void main(String args[]){  
  29. new AEvent();  
  30. }  
  31. }  
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) Example of 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.   
  7. tf=new TextField();  
  8. tf.setBounds(60,50,170,20);  
  9.   
  10. Button b=new Button("click me");  
  11. b.setBounds(100,120,80,30);  
  12.   
  13. Outer o=new Outer(this);  
  14. b.addActionListener(o);//passing outer class instance  
  15.   
  16. add(b);add(tf);  
  17.   
  18. setSize(300,300);  
  19. setLayout(null);  
  20. setVisible(true);  
  21. }  
  22. public static void main(String args[]){  
  23. new AEvent2();  
  24. }  
  25. }  
  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) Example of event handling by Annonymous 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.   
  11. b.addActionListener(new ActionListener(){  
  12. public void actionPerformed(){  
  13. tf.setText("hello");  
  14. }  
  15. });  
  16. add(b);add(tf);  
  17. setSize(300,300);  
  18. setLayout(null);  
  19. setVisible(true);  
  20. }  
  21. public static void main(String args[]){  
  22. new AEvent3();  
  23. }  

No comments:

Post a Comment