Java AWT Event Handling
Java AWT Event Handling
What is an Event?
Change in the state of an object is known as event i.e. event describes the change in state of source. Events are
generated as result of user interaction with the graphical user interface components. For example, clicking on a
button, moving the mouse, entering a character through keyboard, selecting an item from list, scrolling the page are
the activities that causes an event to happen.
Types of Event
The events can be broadly classified into two categories:
Foreground Events - Those events which require the direct interaction of user.They are generated as
consequences of a person interacting with the graphical components in Graphical User Interface. For
example, clicking on a button, moving the mouse, entering a character through keyboard,selecting an item
from list, scrolling the page etc.
Background Events - Those events that require the interaction of end user are known as background
events. Operating system interrupts, hardware or software failure, timer expires, an operation completion
are the example of background events.
Callback Methods
These are the methods that are provided by API provider and are defined by the application programmer and
invoked by the application developer. Here the callback methods represents an event method. In response to an
event java jre will fire callback method. All such callback methods are provided in listener interfaces.
If a component wants some listener will listen to it's events the the source must register itself to the listener.
import java.awt.*;
import java.awt.event.*;
public AwtControlDemo(){
prepareGUI();
awtControlDemo.showEventDemo();
mainFrame.setSize(400,400);
mainFrame.addWindowListener(new WindowAdapter() {
System.exit(0);
});
headerLabel.setAlignment(Label.CENTER);
statusLabel.setAlignment(Label.CENTER);
statusLabel.setSize(350,100);
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
okButton.setActionCommand("OK");
submitButton.setActionCommand("Submit");
cancelButton.setActionCommand("Cancel");
okButton.addActionListener(new ButtonClickListener());
submitButton.addActionListener(new ButtonClickListener());
cancelButton.addActionListener(new ButtonClickListener());
controlPanel.add(okButton);
controlPanel.add(submitButton);
controlPanel.add(cancelButton);
mainFrame.setVisible(true);
else {
statusLabel.setText("Cancel Button clicked.");
Compile the program using command prompt. Go to D:/ > AWT and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AwtControlDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AwtControlDemo
EventListener interface
It is a marker interface which every listener interface has to extend.This
class is defined in java.util package.
Class declaration
Following is the declaration for java.util.EventListener interface:
public interface EventListener
1 ActionListener
2 ComponentListener
3 ItemListener
4 KeyListener
5 MouseListener
6 TextListener
8 AdjustmentListener
9 ContainerListener
10 MouseMotionListener
11 FocusListener
You can tell what kinds of events a component can fire by looking at the kinds of event listeners you can
register on it. For example, the JComboBox class defines these listener registration methods:
addActionListener
addItemListener
addPopupMenuListener
Thus, a combo box supports action, item, and popup menu listeners in addition to the listener methods it
inherits from JComponent.
Because all Swing components descend from the AWT Component class, you can register the following listeners on any Swing component:
component listener
Listens for changes in the component's size, position, or visibility.
focus listener
Listens for whether the component gained or lost the keyboard focus.
key listener
Listens for key presses; key events are fired only by the component that has the current keyboard focus.
mouse listener
Listens for mouse clicks, mouse presses, mouse releases and mouse movement into or out of the component's drawing area.
mouse-motion listener
Listens for changes in the mouse cursor's position over the component.
mouse-wheel listener
Listens for mouse wheel movement over the component.
Hierarchy Listener
Listens for changes to a component's containment hierarchy of changed events.
Hierarchy Bounds Listener
Listens for changes to a component's containment hierarchy of moved and resized events.
All Swing components descend from the AWT Container class, but many of them are not used as containers. So, technically speaking, any Swing component can
fire container events, which notify listeners that a component has been added to or removed from the container. Realistically speaking, however, only containers (such as
panels and frames) and compound components (such as combo boxes) typically fire container events.
JComponent provides support for three more listener types. You can register an ancestor listener to be notified when a component's containment ancestors are added
to or removed from a container, hidden, made visible, or moved. This listener type is an implementation detail which predated hierarchy listeners.
The other two listener types are part of the Swing components' conformance to the JavaBeans specification. Among other things, this means that Swing components support
bound and constrained properties and notify listeners of changes to the properties. Property change listeners listen for changes to bound properties and are used by several
Swing components, such asformatted text fields, to track changes on a component's bound properties. Also, property change listeners, as well as vetoable change listeners are
used by builder tools to listen for changes on constrained properties. For more information refer to the Properties lesson in the JavaBeans trail.
The following table lists Swing components and the specialized listeners they support, not including listeners supported by all Components, Containers,
orJComponent s. In many cases, the events are fired directly from the component. In other cases, the events are fired from the component's data or selection model.
To find out the details for the particular component and listener you are interested in, go first to the component how-to section, and then if necessary to the listener how-to
section.