Event Handling in AWT-


Even Handling is the mechanism of processing the events generated by any source to perform some specific task.
An event is nothing but a state change in source object. For example pressing the push button, entering a key via keyboard, press mouse button etc.
Java makes use of Delegation Event Model to handle events -

Delegation Event Model -

Delegation event model consist of three main components -
  • Event - In java event is an object that describes the state change in source object. For example user enters mouse button, or press button etc.
  • Event Source - It is an object that is responsible for generating events. Event is generated by Event Source whenever someone makes changes in source state.
  • Event Listener ? It is the object that is responsible for handling and processing the events generated by event source.
In order to handle the events generated by Source, Event Listener must first register with the source in order to get notified whenever that Source generates event. Otherwise Event Listener will not be notified about event.
Event Listener should also implements appropriate methods to handle and process the events generated by Event Source.

EventObject is the superclass of all windows events and it is available in java.util package.
EventObject class has two methods -
  • getSource() - It returns the source object of event. It has following syntax -
       public Object getSource()
            
  • toString() - It returns the string representation of source object. It has the following syntax -
            public String toString()
            


java.awt.event package provides several classes and interface for handling events in java. These are given below -
S.No Source of Event(AWT Controls) Event Class Listener Interface Listener Interface Methods to be Implement Event Class Methods Extra Information
1 Button,
Menu,
MenuItem,
TextField
ActionEvent ActionListener actionPerformed(ActionEvent e) e.getSource(),
e.getActionCommand()
2 Choice,
List,
Checkbox,
RadioButton
ItemEvent ItemListener itemStateChanged(ItemEvent e) Control.getState()
3 Scrollbar AdjustmentEvent AdjustmentListener adjustmentValueChanged(
AdjustmentEvent e)
Control.getValue()
4 All Controls FocusEvent FocusListener focusGained(FocusEvent e),
focusLost(FocusEvent e)
5 All Controls KeyEvent KeyListener keyPressed(KeyEvent e),
keyReleased(KeyEvent e),
keyTyped(KeyEvent e)
e.getKeyCode(),
e.setKeyCode()
6 All Controls MouseEvent MouseListener mousePressed(MouseEvent e),
mouseReleased(MouseEvent e),
mouseChecked(MouseEvent e),
mouseEntered(MouseEvent e),
mouseExited(MouseEvent e)
e.getX(),
e.getY(),
e.getClickCounts(),
e.getButton()
7 All Controls MouseEvent MouseMotionListener mouseMoved(MouseEvent e),
mouseDragged(MouseEvent e)
8 Frames/Windows WindowEvent WindowListener windowOpened(WindowEvent e),
windowClosed(WindowEvent e),
wndowClosing(WindowEvent e),
windowActivated(WindowEvent e),
windowDeactivated(WindowEvent e),
windowIconfied(WindowEvent e),
windowDeiconfied(WindowEvent e)
9 MouseWheelEvent MouseWheelListener
10 TextEvent TextListener
11 ComponentEvent ComponentListener
11 ContainerEvent ContainerEvent

Event Classes and Interfaces in detail -

ActionEvent -

ActionEvent is generated for Button, Menu, MenuItem and TextField controls.
It has several methods but most commonly used ones are as follows -
  • getActionCommand() - It returns the ActionEvent object command name. For example if user press a button, an ActionEvent is generated and by default Button command name is same as Button label name. It has the following syntax -
        public String getActionCommand()
    
  • getWhen() - It returns the time at which event took place. It has the following syntax -
        public long getWhen()
    
  • getSource() - Inherited from superclass EventObject, It is used to get the Source (Event generator) Object. It has the following syntax -
        public Object getSource()
    

ActionListener Interface -

The mapped listener interface for ActionEvent class is ActionListener. i.e the classes that generate ActionEvent should implement this interface.
This interface defines one method to handle the events generated by source -
public void actionPerformed(ActionEvent e)

Example -

Addition and subtraction of 2 numbers -

When user will click on Addition button it will add two numbers entered in first two text fields and if he clicks on Subtraction button it will subtract those two numbers.

Different methods to handle Event -

Whenever any source control generates an event it is handled by some listener interface. These listener interfaces have some methods for handling events.
We can put all this event handling code at one of the following three places -
1- Within the same class that generates event.
2- Within inner class.
3- Within anonymous class.
1- Within the same class that generates event -
import java.awt.*;
import java.applet.*;
import java.awt.event.*;    // for event classes and listener interfaces
public class EventDemo1 extends Applet implements ActionListener   // same class implements the listener interface. 
{
    Label l1,l2,l3;
    TextField t1,t2,t3;
    Button b1, b2;
    public void init()
    {
        l1 = new Label("Enter 1st Number :");
        l2 = new Label("Enter 2nd Number :");
        l3 = new Label("Result:");
        t1 = new TextField(30);
        t2 = new TextField(30);
        t3 = new TextField(30);
        b1 = new Button("Addition");
        b2 = new Button("Subtraction");
        add(l1);
        add(t1);
        add(l2);
        add(t2);
        add(l3);
        add(t3);
        add(b1);
        add(b2);
        b1.addActionListener(this);   
        b2.addActionListener(this);
/* ActionListeners register itself for button click. this-  indicates the ?object of current class? i.e. the same class is going to implement the ActionListener interface.   */ 
    }

    public void actionPerformed(ActionEvent e)
    {
        try
        {
            float a = Float.parseFloat(t1.getText());
            float b = Float.parseFloat(t2.getText());
            if(e.getSource() == b1)   // Or if(e.getActionCommand.equals("Addition"))
            {
                t3.setText(" "+(a+b));
            }
            else
            {
                t3.setText(" "+(a-b));
            }

        }
        catch(Exception ex)
        {
            t3.setText(ex.getMessage());
        }

    }
}
        

Output -


When we click on button, it generates an ActionEvent that can be handled and processed by ActionListener Interface.
In our example we need to perform addition and subtraction on button click i.e. ActionEvent will generate when we click on any of the two buttons and to handle that event our class has implemented ActionListener interface. That defines the actionPerformed() method where we define the code to handle the generated event.

2- Within inner class -

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class EventDemo1 extends Applet
{
    Label l1,l2,l3;
    TextField t1,t2,t3;
    Button b1, b2;
    public void init()
    {
        l1 = new Label("Enter 1st Number :");
        l2 = new Label("Enter 2nd Number :");
        l3 = new Label("Result                       :");
        t1 = new TextField(30);
        t2 = new TextField(30);
        t3 = new TextField(30);
        b1 = new Button("Addition");
        b2 = new Button("Subtraction");
        add(l1);
        add(t1);
        add(l2);
        add(t2);
        add(l3);
        add(t3);
        add(b1);
        add(b2);
        b1.addActionListener(new InnerDemo());  
        b2.addActionListener(new InnerDemo());
// Here we passed object of a inner class named InnerDemo
    }

class InnerDemo implements ActionListener   
// Inner class implementing the interface.
{
    public void actionPerformed(ActionEvent e)
    {
        try
        {
            float a = Float.parseFloat(t1.getText());
            float b = Float.parseFloat(t2.getText());
            if(e.getSource() == b1)   // Or if(e.getActionCommand.equals("Addition"))
            {
                t3.setText(" "+(a+b));
            }
            else
            {
                t3.setText(" "+(a-b));
            }

        }
        catch(Exception ex)
        {
            t3.setText(ex.getMessage());
        }

    }
}
}
    

3- Within anonymous class -

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class EventDemo1 extends Applet
{
    Label l1,l2,l3;
    TextField t1,t2,t3;
    Button b1, b2;
    public void init()
    {
        l1 = new Label("Enter 1st Number :");
        l2 = new Label("Enter 2nd Number :");
        l3 = new Label("Result                       :");
        t1 = new TextField(30);
        t2 = new TextField(30);
        t3 = new TextField(30);
        b1 = new Button("Addition");
        b2 = new Button("Subtraction");
        add(l1);
        add(t1);
        add(l2);
        add(t2);
        add(l3);
        add(t3);
        add(b1);
        add(b2);
        b1.addActionListener(new ActionListener()    // Anonymous class implements the listener
        {
           public void actionPerformed(ActionEvent e)
            {
               try
                {
                  float a = Float.parseFloat(t1.getText());
                  float b = Float.parseFloat(t2.getText());
                  t3.setText(" "+(a+b));
               }
             catch(Exception ex)
              {
                t3.setText(ex.getMessage());
               }
         }
     });
     
   b2.addActionListener(new ActionListener()  // Anonymous class implements the listener
    {
     public void actionPerformed(ActionEvent e)
      {
       try
        {
            float a = Float.parseFloat(t1.getText());
            float b = Float.parseFloat(t2.getText());
            t3.setText(" "+(a-b));
        }
        catch(Exception ex)
        {
            t3.setText(ex.getMessage());
        }
     }
});
 }

}
}

We can use any of the three options available.

ItemEvent -

ItemEvent is generated for Choice, List, Checkbox and RadioButton controls.
It has several methods but most commonly used ones are as follows -
  • getItem() - It returns the item that generate the event. It has the following syntax -
    public Object getItem() 
    
  • getItemSelectable() - The getItemSelectable( ) method can be used to obtain a reference to the ItemSelectable object that generated an event. Its general form is shown here -
    public ItemSelectable getItemSelectable( ) 
    
    Lists and choices are examples of user interface elements that implement the ItemSelectable interface.

ItemListener Interface -

The mapped listener interface for ItemEvent class is ItemListener. i.e the classes that generate ItemEvent should implement this interface. This interface defines one method to handle the events generated by source -
    public void  itemStateChanged(ItemEvent e)

Example -

Here we will add four radio buttons and whenever user clicks on any radio button the image corresponding to that radio button will be displayed -
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class EventDemo2 extends Applet implements ItemListener
{
    CheckboxGroup cg;
    Checkbox r1,r2,r3,r4;
    String image = "A.jpg";
    public void init()
    {
        cg = new CheckboxGroup();
        r1= new Checkbox("A.jpg", cg, true);
        r2= new Checkbox("B.jpg", cg, false);
        r3= new Checkbox("C.jpg", cg, false);
        r4= new Checkbox("D.jpg", cg, false);
        add(r1);
        add(r2);
        add(r3);
        add(r4);
        r1.addItemListener(this);
        r2.addItemListener(this);
        r3.addItemListener(this);
        r4.addItemListener(this);

    }

    public void itemStateChanged(ItemEvent e)
    {
        if(r1.getState())  // returns true if r1 is selected
        {
            image = r1.getLabel();
        }
        if(r2.getState())
        {
          image = r2.getLabel();
        }
        if(r3.getState())
        {
          image = r3.getLabel();
        }
        if(r4.getState())
        {
          image = r4.getLabel();
        }
        repaint();
    }

    public void paint(Graphics g)
    {
        Image img = getImage(getCodeBase(), image);
        g.drawImage(img, 30, 30, 300, 300, this);
    }
}

Adjustment Event -

AdjustmentEvent is generated for Scrollbar controls. It has several methods but most commonly used ones are as follows -
  • getValue() - It returns the current value in adjustment event. It has the following syntax -
    public int getValue()
    
  • getAdjustable() - ? It returns the object of current source which generate the event.

AdjustmentListener Interface -

The mapped listener interface for AdjustmentEvent class is AdjustmentListener. i.e the classes that generate AdjustmentEvent should implement this interface. This interface defines one method to handle the events generated by source -
public void  adjustmentValueChanged(AdjustmentEvent e)

Example -

Here we will add 3 scrollbars and according to the values of scrollbars background color of applet will change.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class EventDemo3 extends Applet implements AdjustmentListener
{
    Scrollbar s1, s2, s3;
    public void init()
    {
         setLayout(null);
        s1 = new Scrollbar(0,255,10,0,265);
        s2 = new Scrollbar(0,255,10,0,265);
        s3 = new Scrollbar(0,255,10,0,265);
        s1.setBounds(20, 20, 300,20);
        s2.setBounds(20, 50, 300,20);
        s3.setBounds(20, 80, 300,20);
        add(s1);
        add(s2);
        add(s3);
        s1.addAdjustmentListener(this);
        s2.addAdjustmentListener(this);
        s3.addAdjustmentListener(this);
    }

    public void adjustmentValueChanged(AdjustmentEvent e)
    {
        int red = s1.getValue();
        int green = s2.getValue();
        int blue = s3.getValue();

        Color c = new Color(red, green, blue);
        setBackground(c);
    }
}
        

FocusEvent -

Focus Event is generated for all controls whenever you focus (i.e. point mouse cursor on that control) on that control.

FocusListener Interface -

The mapped listener interface for FocusEvent class is FocusListener. i.e. the classes that generate FocusEvent should implement this interface. This interface defines two methods to handle the events generated by source -

public void focusGained(FocusEvent e)
public void focusLost(FocusEvent e)    

Example -

Here we are using 2 text fields one for user name and one for mobile number; we will ensure that user should not enter anything other than numbers in Mobile text field by restricting user to enter only numbers.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class EventDemo4 extends Applet implements FocusListener
{
    TextField t1, t2;
    Label l1, l2;
    public void init()
    {
        t1 = new TextField(40);
        t2 = new TextField(40);
        l1 = new Label("Mobile: ");
        l2 = new Label("User name:");
        add(l1);
        add(t1);
        add(l2);
        add(t2);
        t1.addFocusListener(this);

    }

    public void focusGained(FocusEvent e)
    {

    }
    public void focusLost(FocusEvent e)
    {
        try
        {
            int a = Integer.parseInt(t1.getText()); 
// If user enter something other than integers it will throw an exception
        }
        catch(Exception ex)
        {
            t1.requestFocus();  
/*  we request focus back on t1 text field i.e. cursor remain in t1 text field if t1 contains anything other than integers.    */
        }

    }
}

Output -



Adapter Classes -

Java provides us Adapter classes for all the listener interfaces that have more than one method. Adapter classes have default implementation for all the methods of listener interfaces. So in the case where you do not need to implements all the methods of Listener interface, there you can use corresponding Adapter class in place of Listener interface and it will save your code and time because Adapter class will provide the implementation of remaining methods.
The listener interface with their corresponding Adapter classes is given below-
Listener Interface Adapter Class
FocusListener FocusAdapter
KeyListener KeyAdapter
MouseListener MouseAdapter
MouseMotionListener MouseMotionAdapter
WindowListener WindowAdapter
ComponentListener ComponentAdapter
ComponentAdapter ContainerAdapter

Example -

Here we are using 2 text fields one for user name and one for mobile number; we will ensure that user should not enter anything other than numbers in Mobile text field by restricting user to enter only numbers.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class EventDemo5 extends Applet
{
    TextField t1, t2;
    Label l1, l2;
    public void init()
    {
        t1 = new TextField(40);
        t2 = new TextField(40);
        l1 = new Label("Mobile:");
        l2 = new Label("User name:");
        add(l1);
        add(t1);
        add(l2);
        add(t2);
        t1.addFocusListener(new FocusAdapter()
        {
          public void focusLost(FocusEvent e)
          {
            try
             {
               int a = Integer.parseInt(t1.getText());
             }
          catch(Exception ex)
           {
            t1.requestFocus();
           }
         }
    });
}
}
        

Output -


We will use Adapter classes in the remaining section of event handling. To see more examples of Adapter classes please refer the remaining section of event handling.

KeyEvent -

KeyEvent is generated for all controls whenever you pressed, released or typed any key from the keyboard.
KeyEvent class also defines several integer constants that we can use while working with KeyEvent class. Some of commonly used one are as follows -
VK_0 to VK_9 - represents ASCII equivalent of 0 to 9 numbers.
VK_A to VK_Z - represents ASCII equivalent of letters.
Some others are -
VK_SHIFT, VK_CANCEL, VK_BACK_SPACE, VK_SPACE,VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN, VK_ENTER, VK_ALT, VK_PAGE_UP, VK_PAGE_DOWN etc.
Here VK is a constant that specify virtual key codes.
KeyEvent has several methods but most commonly used are as follows -
  • getKeyCode()- It returns the key code that is entered by user. It has the following syntax -
    public int getKeyCode()
    
  • setKeyCode()- It sets the key code to the specified one. It has the following syntax -
    public void setKeyCode(int keycode)
    
  • getKeyChar()- It sets the key char to the specified one. It has the following syntax -
    public char getKeyChar()
    
  • setKeyChar()- It returns the character user enered. It has the following syntax -
    public void setKeyChar(char keychar)
    

KeyListener Interface -

The mapped listener interface for KeyEvent class is KeyListener. i.e. the classes that generate KeyEvent should implement this interface. This interface defines three methods to handle the events generated by source-
public void keyPressed(KeyEvent e) 
It invokes when a key is pressed from keyboard.    
public void keyRealesed(KeyEvent e)
It invokes when key is released.
public void keyTyped(KeyEvent e)
It invokes when a key is pressed and it generates a character. Remember, not all key press result in character.
For example, pressing space key does not generate a character.

Example 1 -

Here we will add 2 text fields one for mobile number and one for user name. Here user is allowed to use only 0 to 9 and back space key for mobile number text field and A to Z , back space and space key for User name text field. No other key will be allowed in any of the two text fields.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class EventDemo2 extends Applet
{
    TextField t1, t2;
    Label l1, l2;
    public void init()
    {
        l1 = new Label("Mobile Number:");
        l2 = new Label("User Name:");
        t1 = new TextField(25);
        t2 = new TextField(25);
        add(l1);
        add(t1);
        add(l2);
        add(t2);
        t1.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e)
            {
                int a = e.getKeyCode();
                if(!((a>= 48 && a <= 57)|| a == KeyEvent.VK_BACK_SPACE))
                {
                    e.setKeyCode(KeyEvent.VK_CANCEL);
                }
            }
        });
        t2.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e)
            {
                int a = e.getKeyCode();
                if(!((a >= 65 && a <= 90) || a == KeyEvent.VK_BACK_SPACE || a == KeyEvent.VK_SPACE))
                {
                    e.setKeyCode(KeyEvent.VK_CANCEL);
                }
            }

        });
    }
}
    

Example 2 -

Here we will add a text area and a button. Here whatever you typed in text area, it will show you a constant text string again and again. And when you click on button it will show the original text.
    
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class EventDemo2 extends Applet
{
    String disp = "THIS IS JUST A DEMO";
    int position = 0;
    TextArea ta = new TextArea(10,30);
    String str = "";
    Button b1 = new Button("Load Orignal");
    public void init()
    {
        add(ta);
        add(b1);
        ta.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e)
            {
                int v = e.getKeyCode();
                str = str + (char)v;
                e.setKeyCode(disp.charAt(position));
                position = (position + 1)%disp.length();
            }
        });
        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                ta.setText(str);
            }
        });
    }
}

Output -


After pressing the button, it will show you original text -

MouseEvent -

MouseEvent is generated for all controls whenever you pressed, released, moved, dragged etc. mouse. It has several methods but most commonly used one are as follows-
  • getX() - It returns the horizontal x coordinate positions of mouse cursor within the component. It has the following syntax -
    public int getX()
    
  • getY() - It returns the vertical y coordinate positions of mouse cursor within the component. It has the following syntax -
    public int getY()
    
  • getClickCount()- It returns how many time you click the mouse. It has the following syntax -
    public int getClickCount()
    
  • getButton()- It returns an integer value that denotes the mouse button due to which event is generated. It has the following syntax -
    public int getButton()
    It can return one of the following values -
    0- (NOBUTTON) - No button is pressed.
    1- (BUTTON1)  - left mouse button
    2- (BUTTON2)
    3- (BUTTON3)
    

MouseListener and MouseMotionListener Interface -

The mapped listener interface for MouseEvent class are MouseListener and MouseMotionListener interface. i.e. the classes that generate MouseEvent should implement one of these interface.
MouseListener interface defines following methods to handle the events generated by source -
  • public void mousePressed(MouseEvent e) - It invokes when mouse button is pressed.
  • public void mouseReleased(MouseEvent e) - It invokes when mouse button is released.
  • public void mouseClicked(MouseEvent e)- It invokes when mouse button is released i.e. pressed and released.
  • public void mouseEntered(MouseEvent e) - It invokes when mouse cursor entered a component.
  • public void mouseExited(MouseEvent e) - It invokes when mouse cursor exited from component.
MouseMotionListener interface defines following methods to handle the events generated by source -
  • public void mouseDragged(MouseEvent e) - It invokes when mouse is dragged (move mouse while mouse button is pressed).
  • public void mousMoved(MouseEvent e) - It invokes when mouse is moved.

Example 1 -

This example will draw a line from one point to another. Line starts drawing when user pressed mouse left button and end where user released the button.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class EventDemo6 extends Applet
{
    int x1, y1, x2, y2;
    public void init()
    {
        this.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e)
            {
                if(e.getButton() == 1) // 1 denotes mouse left button
                {
                    x1 = e.getX();
                    y1 = e.getY();
                }
            }

            public void mouseReleased(MouseEvent e)
            {
                if(e.getButton() == 1)
                {
                    x2 = e.getX();
                    y2 = e.getY();
                    repaint();
                }
            }

        });
    }
    public void update(Graphics g)
    {
        paint(g);
    }
    public void paint(Graphics g)
    {
        g.drawLine(x1, y1, x2, y2);
    }
}    

Output -

Example 2 -

This example will display a message with mouse cursor where ever you moved mouse cursor.

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class EventDemo7 extends Applet
{
    int x, y;
    public void init()
    {
        this.addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseMoved(MouseEvent e)
            {
                x = e.getX();
                y = e.getY();
                repaint();
            }
         });
    }
    public void paint(Graphics g)
    {
        g.drawString("This Is My Mouse Cursor", x, y);
    }
}        
 

Output -


WindowEvent -

WindowEvent is generated for Frame/Windows whenever windows status (i.e. window opened, closed, activated, deactivated etc.) changed. It has several methods but most commonly used are as follows -
  • getNewState()- It returns the new state of window after event. It has the following syntax -
    public int getNewState()
    
  • getOldState()- It returns the old state of window after event. It has the following syntax -
    public int getOldState()
    
  • getWindow()- It returns the windows that caused the event. It has the following syntax -
    public Window getWindow()
    

WindowListener Interface -

The mapped listener interface for WindowEvent class is WindowListener i.e. the classes that generate WindowEvent should implement this interface.
WindowListener interface defines following methods to handle the events generated by source -
  • public void windowOpened(WindowEvent e) - It invokes when window is opened for the first time.
  • public void windowClosed(WindowEvent e) - It invokes when window has closed.
  • public void windowClosing(WindowEvent e) - It invokes while the window is closing i.e. while user is attempting to close the window.
  • public void windowActivate(WindowEvent e) - It invokes when windows becomes active one i.e. window gets the focus back.
  • public void windowDeactivated(WindowEvent e) - It invokes when window is no longer active one i.e. window lost the focus.
  • public void windowIconified(WindowEvent e) - It invokes when window status changed from normal state to minimized state.
  • public void windowDeiconified(WindowEvent e) - It invokes when window status changed from minimized to normal state.

Example -

This example will draw a frame and use WindowListener. We will cover Frame in detail in next section.
import java.awt.*;
import java.awt.event.*;
public class Test 
{
    public static void main(String args[])
    {
        Frame fr = new Frame("My Frame");  
       // create a frame with title ?My Frame?
        fr.setVisible(true);  
      // to make frame visible, by default invisible
        fr.setSize(400, 400);
     // set width and height of frame
        fr.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);  // to close the frame when try to close it.
            }

        });
    }
}
            

Output -


MouseWheelEvent -

It is a subclass of MouseEvent. Mouse wheel is located between mouse buttons. Not all mice have wheels. Mouse wheel is used for scrolling.

MouseWheelListener Interface -

The mapped listener interface for MouseWheelEvent class is MouseWheelListener i.e. the classes that generate MouseWheelEvent should implement this interface. MouseWheelListener interface defines following method to handle the events generated by source -
  • public void mouseWheelMoved(MouseWheelEvent e) -
  • It invokes when mouse wheel is moved.

TextEvent -

TextEvent is generated for TextField and TextArea controls whenever characters entered by user.

TextListener Interface -

The mapped listener interface for TextEvent class is TextListener i.e. the classes that generate TextEvent should implement this interface.
TextListener interface defines following method to handle the events generated by source -
  • public void textChanged(TextEvent e) - It invokes when user changes the text within TextField or TextArea control.