- What is Java?
- History of Java
- Hello Java Program
- Features of Java
- Basic Syntax
- Java Setup
- Data Types in Java
- Java Variables
- Operators in Java
- Java If-else Statement
- Switch Case Statement
- Java Loops
- Do-While Loop
- Java While Loop
- Continue Statement
- Break Statement in Java
- Constructors in Java
- Oops Concept in Java
- Features of OOPs
- Exeception handeling
- Aggregation (HAS-A relationship) in Java
- Method Overriding in Java
- Method Overloading
- Java Static Keyword
- Java This Keyword
- Java Final Keyword
- Static Binding and Dynamic Binding
- Abstract class in Java
- Access Modifiers in Java
- Difference between abstract class and interface
- Interface in Java
- Garbage Collection in Java
- Java Package
- Serialization and Deserialization in Java
- Java Inner Classes
- Java Applets
- Multithreading in Java
- Thread Priorities in Java
- Thread Creation
- Inter Thread Communication
- Wrapper Classes in Java
- Java Input Output
- Java AWT Introduction
- Java Layout Manager
- Java Layout Policy
- Java AWT Events
- Collection Framework
- Collection Framework List Interface
- Swing in Java
- Swing Utility Classes
- Swing Layout Managers
- Java JDBC
Java AWT EVENTS
When a normal Java program (which is not GUI based) needs input, it prompts the user and then calls some input method, such as readline() i.e. the interaction is initiated by the program. This is not the way in which gui-based programs behave. The user initiates interaction with the program rather than program initiating the action.
For example, in a word processing software, user initiates action by clicking on different buttons, which generates an event and some piece of code is executed as a result and accordingly some action takes place. The clicking on save button will invoke the method which saves the contents to a file.
|MouseMotionListener||mouseMoved||MouseEvent me||same as prev.|
Java uses event delegation model for event handling. In the event delegation model a component may be told which object or should be notified when the component generates a particular kind of event.
If a component is not interested in an event type, then events of that type will not be propagated. The delegation model is based on the following key concepts:
- Event classes.
- Event listeners.
Clicking on a button results in an event. The event has lot of information associated with it, like: time, component generating the event, co-ordinates of the point at which clicking took place etc.
This information is put inside an event class. The concept is similar to exception handling. Whenever an exception occurs an object of appropriate exception class is created and all the exception related information is put into this object.
We have hierarchy of event classes, which is somewhat similar to the hierarchy of exception classes. Whenever some event occurs, it is detected by event handling, mechanism, which creates objects of appropriate event class and puts all the event related information into it.
Event listeners are responsible for taking appropriate action when an event occurs. An event listener is an object to which a component has delegated the task of handling a particular kind of event.
When the component experiences input, an event of the appropriate type is constructed, the event is then passed as the parameter to a method call on the listener. A listener must implement the interface that contains that event handling method.
We can compare the event listeners with the catch blocks for handling the exceptions. We need to provide a catch block corresponding to the exception to be handled. Similarly if we want to handle an event generated by some component, we need to provide appropriate listener.
The listeners are associated with the components. Different events have different listeners. When event occurs, an appropriate listener method is called and event object is passed as parameter.
The listeners defined in the language are interfaces only. We need to provide implementation of the appropriate listener. Some listeners have 2 or more methods.
To implement a listener having more than one method, we need to provide implementation of all the methods even if we are interested only in one method. The adapter classes provide the solution to this problem.
An adapter class provides dummy implementation of all the methods of a listener. This helps in reducing programming efforts. If we want to use just one method of a listener class containing say 5 methods then we can simply extent the adapter class and override the desired method.
The AWT has a well-defined event hierarchy. Normally the classes corresponding to leaf nodes in the event hierarchy represent the actual events. While non-leaf classes act as base classes and encapsulate.
AWT event hierarchy is described in the following diagram.
The java.util.eventObject class:
This is the top most super class of all the event classes. It is a very general class, with only one method of interest, which returns the object that originated the event.
The java.awt.AWTEvent Class:
One sub class of event object is java.awt.AWTEvent, which is the super class of all the delegation model event classes. Again, there is only one method of interest, which return the id of the event.
An event’s ID is an int that specifies the exact nature of the event. For example, an instance of the mouseevent class can represent one of seven occurrences: click , Drag, Entrance, Move, Exit, Press, Release. Each of these possibilities is represented by an int MouseEvent . MOUSE_CLICKED, MouseEvent.MOUSE_DRAGGED, and so on.
The sub classes of java.awt.AWTEvent represent the various event types that can be generated by the various AWT components, and contain all necessary information relating to the activity that triggered the event.
The non-super class event types (i.e. those that are actually fired by component) are:
general by activation of components for example .it is generated on button click, on double-click on list item and on pressing enter in a TextField.
generated by adjustment of adjustable components such as scroll bars.
generated when components are added to or removed from a container.
generated when a component receives or loses input focus.
generated when an item is selected from a list, choice, or checkbox.
generated by keyboard activity.
generated by mouse activity.
An event, which indicates that the mouse wheel was rotated in a component.
generated by window activity (such as iconifying or deiconifying.)
generated when a text component is modified.
The java.awt.event package:
This package provides the interfaces and classes for dealing with different types of events fired by AWT components. The events are fired by event sources. An event listener registers with an event source to receive notifications about the events of a particular type.
This package defines events and event listeners, as well as event listener adapters, which are convenience classes to make easier the process of writing event listeners.
Event Listener Interfaces:
Each event class has a corresponding listener interface and defines one or more methods. The most commonly used listener interfaces are described in the following sections.
The Action Listener Interface:
This is the listener interface for receiving action events. The class that is interested in processing an action event implement this interface, and the object created with that class is registered with a component, using the component’s addActionListener() method. When the action event occurs, the action listener’s action performed () method is invoked whose signatures are: