Introduction
GUI provides an interface through which a user interacts with the application. It is necessary that the application responds to the actions performed by the user.
The user actions are represented through events in the Java programming language. To handle these events, Java provides a mechanism event model.
An event is handled by defining and associating a handler for the event. Defining a handler enables the application to respond to the event, and associating a handler enables the application to bind the handler with the event.
The event model contains three components:
- Event Source
- Event listener
- Event handler
Event Source
An event source is an object that generates an event. The object registers some handler, which receives notifications about a specific type of event. There are various components in the Swing toolkit such as button, frame, and combo box, which act as an event source. These sources generate various events, for example, a button generates an action event when the button is clicked, or a frame generates the window event when the window is opened or closed.
Event Listener
An event listener listens for a specific event and gets notified when the specific events occur. An event listener registers with one or more event sources to receive notifications about specific types of events and to process the events. The information may contain the source that generated the event and the time when the event was generated.
Event handler
An event handler is a method that is invoked by an event listener when the event is generated. Event listeners are the interfaces and event handlers are the methods declared in the event listener interface that is implemented by the application.
Event classes
Event classes represent the characteristic of all events, such as source, time, and ID of the event. When an event occurs, the related object of the event is created.
The event classes are:
- Action Event
- KeyEvent
- Mouse Event
- FocusEvent
- Item Event
- Window Event
The Event object class, which is in the Java.util package, is the superclass for all the event classes. The class, Event Object, from which all event objects are derived, extends the event objects class and implements the serializable interface.
Action Event
The action event is generated by components, such as a button when an action is performed.For example, clicking a button is an action event generated by the button.
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JLabel;
- import javax.swing.JFrame;
- import javax.swing.Jpanel;
- public class ActionEventDemo extends JFrame implements ActionListener {
- JButton click;
- JPanel Panle;
- JLabel message;
- public ActionEventDemo() {
- super("Action Event Demo");
- click = new JButton("Click");
- panel = new JPanel();
- message = new JLabel();
- add(panel);
- panel.add(click);
- panel.add(message);
- setSize(300, 300);
- setVisible(true);
- }
- public static actionPerformed(ActionEvent e) {
- message.setText("Welcome to event handling in java");
- }
- public static void main(String args[]) {
- ActionEventDemo obj = new ActionEventDemo();
- }
- }
Mouse Event
The mouse event is generated by a mouse with respect to the source component. These mouse Events can have two categories,
- Mouse Events
It contains the mouse events, such as mouse pressed, mouse clicked, and mouse entered.
- Mouse motion events
It contains the mouse motion events, such as mouse moved and mouse dragged.
KeyEvent
The key event is generated by a component when the key is pressed, released, or typed within the component. int getKeycode() is a method in keyevent that returns the integer key code for the key and char getKeyChar() is a method that returns the character associated with the key.
FocusEvent
The focus event is generated by a component when it receives or loses focus. boolean isTemporary() is a method which identifies whether the focus is changed permanently or temporarily. Component getOppositeComponent() is a method that returns the opposite component that is involved in focus or activation change.
ItemEvent
The item event is generated by components, such as a radio button, and checkbox, when an item or the component is selected or deselected by the user.
Object getItem() is a method that returns the item affected by the event and int getStateChange() is a method that returns the changed state.
Window Event
The window event is generated by the window when it is opened, closed, activated, deactivated, or when the focus is transferred into or out of the window.
int getNewState()-returns the new state of the window.
int getOldState() -return the previous state of the window.