Java event handling
Event handling in Java refers to executing some code when specific actions occur such as a window being minimized or a button being clicked. There are special interfaces for this purpose and they are located in the java.awt.event package.
This lesson focuses on:
- Handling window events
- Handling action events
- Handling focus events
- Handling item events
- Handling adjustment events
- Handling text events
- Handling mouse events
- Handling key events
- Making objects listen for events
- Using Inner Adapter classes
Handling window events
To handle window events, the WindowListener interface is used. The WindowListener interface has various methods to handle window events.
Methods of the WindowListener interface:
-
windowActivated(WindowEvent e)
This method is called when a window has become active (meaning it is able to accept input events).
-
windowClosed(WindowEvent e)
This method is called when a window is closed.
-
windowClosing(WindowEvent e)
This method is called when a user attempts to close a frame.
-
windowDeactivated(WindowEvent e)
This method is called when a window is deactivated (meaning it has lost input focus).
-
windowDeiconified(WindowEvent e)
This method is called when a window has been restored from a minimized state.
-
windowIconified(WindowEvent e)
This method is called when a window is iconified (minimized).
-
windowOpened(WindowEvent e)
This method is called when a window first becomes visible.
Handling action events
To handle action events (such as the clicking of a button), the ActionListener interface is used.
Methods of the ActionListener interface:
-
actionPerformed(ActionEvent e)
This method is called when an action has occured.
Handling focus events
To handle focus events (such as when an object such as a textbox is awaiting user input), the FocusListener interface is used.
Methods of the FocusListener interface:
-
focusGained(FocusEvent e)
This method is called when an object gains focus.
-
focusLost(FocusEvent e)
This method is called when an object loses focus.
Handling item events
Item events are triggered by components that allow for a selection of items such as Checkboxes, and Lists. To handle item events, the ItemListener interface is used.
Methods of the ItemListener interface:
-
itemStateChanged(ItemEvent e)
This method is called when the state of an item has changed.
Handling adjustment events
Adjustment events are triggered by components that are adjustable such as a scroll bar. To handle adjustment events, the AdjustmentListener interface is used.
Methods of the AdjustmentListener interface:
-
adjustmentValueChanged(AdjustmentEvent e)
This method is called when a component is readjusted.
Handling text events
Text events are triggered by components whose fields are editable such as textboxes. To handle text events, the TextListener interface is used.
Methods of the TextListener interface:
-
textValueChanged(TextEvent e)
This method is called when the data in a field of a component has changed.
Handling mouse events
Mouse events are triggered by the computers mouse. Mouse events are actually handled by two interfaces - MouseListener and MouseMotionListener.
The MouseListener interface is used to detect the clicking of mouse buttons or the moving of a mouse into or out of a listener's area. The MouseMotionListener interface is used to detect the movement of the mouse.
Methods of the MouseListener interface:
-
mouseClicked(MouseEvent e)
This method is called when a mouse button is clicked.
-
mouseEntered(MouseEvent e)
This method is called when the mouse enters a source components area.
Methods of the MouseMotionListener interface:
-
mouseMoved(MouseEvent e)
This method is called when the mouse is moved while in a components area.
-
mouseDragged(MouseEvent e)
This method is called when the mouse moves while a mouse button is down while within a components area.
Handling key events
Key events are triggered by keyboard actions. Key events are handled by the KeyListener interface.
Methods of the KeyListener interface:
-
keyPressed(KeyEvent e)
This method is called when a key is pressed down.
-
keyReleased(KeyEvent e)
This method is called when a key is released.
-
keyTyped(KeyEvent e)
This method is called when a key is typed.
Making objects listen for events
Once you implement the necessary interfaces, and define the implementation of their methods, you need to make objects listen for events. For example, if you implement the ActionListener interface, and you define what its actionPerformed() method does, and you want the method to be called every time a certain button is clicked, you need to make that button listen for the event.
To make windows listen for window events defined by the methods of the WindowListener interface, use the addWindowListener() method.
Example:
import java.awt.*;
import java.awt.event.*;
class FrameWithEvents implements WindowListener{
//the constructor for
//the FrameWithEvents class
public FrameWithEvents(){
Frame aFrame = new Frame();
aFrame.setSize(200, 200);
aFrame.addWindowListener(this);
aFrame.setVisible(true);
}
public static void main(String[] args){
FrameWithEvents FWE = new FrameWithEvents();
}
//specify what should happen when the frame
//first becomes active
public void windowActivated(WindowEvent e){
System.out.println("The frame has been activated");
}
//specify what should happen when the user
//is trying to close the frame
public void windowClosing(WindowEvent e){
System.out.println("The frame is closing.....");
/*
The following line of code
specifies that the frame should be closed.
e.getSource() gets the object that called the
event (the frame), and the dispose() method disposes
of it. The purpose of ((Window) at the beginning of
this line of code is to cast the object that calls
the method into a window
*/
((Window)e.getSource()).dispose();
}
//specify what should happen
//when the frame has been closed
public void windowClosed(WindowEvent e){
System.out.println("The frame has been closed");
System.exit(0);
}
//specify what should happen
//when the frame loses focus
public void windowDeactivated(WindowEvent e){
System.out.println("The frame
has been deactivated");
}
//specify what should happen
//when the frame is restored from a minimized state
public void windowDeiconified(WindowEvent e){
System.out.println("The frame has been restored
from a minimized state");
}
//specify what should happen
//when a frame is minimized
public void windowIconified(WindowEvent e){
System.out.println("The frame has been minimized");
}
//specify what should happen
//when a frame first becomes visible
public void windowOpened(WindowEvent e){
System.out.println("The frame is now visible");
}
}
In the above example, a class named FrameWithEvents that implements the WindowListener interface is created. Within the class definition, there is a class constructor that creates a frame which is 200 by 200, listens for window events, and is initially visible. Each of the WindowListener interfaces methods are used to perform some action when something happens with the window such as when it is first opened or when it is minimized. Within the main() method of this class, a new instance of the FrameWithEvents class is created.
For action events use the addActionListener() method.
Example:
//create a button
Button aButton = new Button("This is a button");
//specify that this button listens for action events
aButton.addActionListener(this);
For focus events use the addFocusListener() method.
Example:
//create a textbox
TextField aTextField = new TextField("This is a textbox");
//specify that this textbox listens for focus events
aTextField.addFocusListener(this);
For item events use the addItemListener() method.
Example:
//create a checkbox
CheckBox chkbox1 = new CheckBox("This is a checkbox", false);
//specify that this checkbox listens for item events
chkbox1.addItemListener(this);
For adjustment events use the addAdjustmentListener() method.
Example:
//create a scrollbar ScrollBar sb1 = new ScrollBar(); //specify that this scrollbar //listens for adjustment listener events sb1.addAdjustmentListener(this);
For text events use the addTextListener() method.
Example:
//create a textbox
TextField aTextField = new TextField("This is a textbox");
//specify that this textbox listens for text events
aTextField.addTextListener(this);
For mouse events use the addMouseListener() method.
Example:
//create a frame Frame aFrame = new Frame(); //specify that this frame listens for mouse events aFrame.addMouseListener(this);
For mouse motion events use the addMouseMotionListener() method.
Example:
//create a frame Frame aFrame = new Frame(); //specify that this frame listens for mouse motion events aFrame.addMouseMotionListener(this);
For key events use the addKeyListener() method.
Example:
//create a textbox
TextField aTextField = new TextField("This is a textbox");
//specify that this textbox listens for key events
aTextField.addKeyListener(this);
NOTE: The purpose of using the this keyword within the method that makes the object listen for events is to specify that the object itself should be affected when the event occurs.
Using Inner Adapter classes
You can also make objects listen for events by using inner adapter classes. Inner adapter classes can be used to facilitate the implementation of various listener interfaces.
Example:
//declare a button
Button aButton = new Button("This is a button");
//specify that this button listens for
//action events using an inner adapter class
aButton.addActionListener(new ActionAdapter(){
public void actionPerformed(ActionEvent e){
count++;
}});
In the above example, a button listens for an action event by implementing an inner adapter class named ActionAdapter.




