Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

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 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. 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. class AEvent extends Frame implements ActionListener{
  4. TextField tf;
  5. AEvent(){
  6. tf=new TextField();
  7. tf.setBounds(60,50,170,20);
  8. Button b=new Button(“click me”);
  9. b.setBounds(100,120,80,30);
  10. b.addActionListener(this);
  11. add(b);add(tf);
  12. setSize(300,300);
  13. setLayout(null);
  14. setVisible(true);
  15. }
  16. public void actionPerformed(ActionEvent e){
  17. tf.setText(“Welcome”);
  18. }
  19. public static void main(String args[]){
  20. new AEvent();
  21. }
  22. }
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.

eventclick

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. tf=new TextField();
  7. tf.setBounds(60,50,170,20);
  8. Button b=new Button(“click me”);
  9. b.setBounds(100,120,80,30);
  10. Outer o=new Outer(this);
  11. b.addActionListener(o);//passing outer class instance
  12. add(b);add(tf);
  13. setSize(300,300);
  14. setLayout(null);
  15. setVisible(true);
  16. }
  17. public static void main(String args[]){
  18. new AEvent2();
  19. }
  20. }
  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. 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. }

 

 

Comments

comments