Event Handling in Java for Beginners
Any action that user performs on a GUI component must be listened and necessary action should to be taken. For example, if a user clicks on a Exit button, then we need to write code to exit the program. So for this, we need to know that the user has clicked the button. This process of knowing is called as listening and the action done by the user is called an event. Writing the corresponding code for a user action is called as Event handling.
An event listener in Java is an interface that contains methods called handlers in which corresponding action code is to be written.
An event class contains the information about an event.
Event source is the GUI component or model on which an event is generated or in other words an action is done.
An adapter class is an abstract class implementing a listener interface. This is essential when we don't want to write all the handlers. For example, MouseListener interface contains a lot of methods such as mousePressed(), mouseReleased().. and we want to write only one of them, we use adapter class. This class implements all the methods of an interface giving them an empty body while itself being abstract.
Dispatching of events
For every action user performs, a corresponding event object is generated. This generated event object should be sent to the corresponding listener so that we can handle that event and write the code accordingly. The process of sending of event object to its corresponding listener is called as event dispatching. Events cannot be dispatched if they aren't generated and an event, except MouseEvent cannot be generated on a disabled component.
Semantic vs Low level eventsLow level events represent direct interaction with the user. They represent low level input such as keyboard or mouse. Here are a list of low level events.
java.awt.event.ComponentEventSemantic events are dependent events i.e. they depend on low level events. Sources of semantic events can be model like a Timer. Examples include ActionEvent, ItemEvent etc.
| +-- java.awt.event.MouseEvent
| +-- java.awt.event.KeyEvent
Event classes and their hierarchy
A GUI component can be registered to multiple listeners either of the same type or of different types supported by it.
Not all GUI components can generate all types of events. For example, a Frame cannot generate an ActionEvent
Event classes are the heart of event handling. They contain the information about the generated event. We need to learn them before we step into the concept. The AWT defines event classes that are also used in most of the Swing components.
The super class of all the events is java.util.EventObject. This class contains getSource() method which returns the source of the generated event. An immediate sub class of EventObject is the AWTEvent class which is the super class of all AWT based events.
Examples on each event
ActionEvent is generated on various AWT components like Button, TextField etc.
This event is generated whenever an item is selected/de-selected on a List or a Choice typically.
- Using ItemListener for AWT Checkbox
- Using ItemListener for AWT RadioButton
- Using ItemListener for AWT Choice
- Using ItemListener for AWT List
This event is generated whenever user presses/releases or types a key.
This event is generated whenever there is a change in text of a text component such as TextField or TextArea.
This is generated whenever user performs an action with the mouse. These include pressing,releasing,clicking,entering,exiting,moving and dragging of mouse.
This is generated whenever user does something with his mouse wheel.
This is generated whenever there is a change in the window state such as minimized,maximized,activate,de-activated,opened,closing,closed.
This event is generated when a component gets the focus i.e. it is selected currently.
This event is generated whenever a component is added or removed to a container.
This event is generated whenever there is a change in the size,location,visibility of a component.
This event is generated whenever a window gets or loses focus.
This event is generated whenever there is a value change in a scrollbar.
If you have time, refer to the official event handling tutorial in Swing as well.