Home | » | Java | » | Advance Java | » | Java Event Handling |
---|
An Event occurs when there is a change in the active state of components such as buttons and text boxes. Examples of events are when the end user clicks a button or enters text in a textbox. Event handling is the process to monitor any change in the state of an object and perform a corresponding action. These actions are created in form of modules or functions and can further lead to events.
Contents
- Event Delegation Approach
- Event Listener
There are Many events those may have Occurred Like Clicking a Button, Moving a Mouse on a Surface and Entering Some Text and Choosing an item from a List and Pressing a Key from Keyboard will cause So Some events So that JAVA Provides also their Associated Listener which will handle these Events those are Explained below.
Action Listener Adjustment Listener Mouse Listener Mouse Motion Listener Window Listener Key Listener Focus Listener ITEM Listener Text Listener - Process of Handling Events
Event Delegation Approach
It defines how user will interact with the components that make up the GUI for a program and how java handle these events. In java events are objects and the java provides a event package Which defines a hierarchy of events. By using this package a user can create instance of various event classes and java will decide how to handle the event that is to be generated. But when a user clicks a button, the system will itself creates an instance of Action Event class which is used for storing the detail about the event. This is also Called as JAVA' event Delegation Model which defines the Standards and Mechanism for handling the events For handling Events in JAVA ,it uses a Event Delegation Model Which Contains various components for handling the events those are occurred.
Source For creating the events there must be a source of event , for example when a button is clicked then it Execute Some Operations or this Executes some Programs and display some Results. So that here a button is the source of the event.
Every program will have a interface named action listener that is to be implement from the java.awt.event package and it relates to event class who will listen the code that is associated with a button. When an event if fire by a user then the event goes to the Particular Listener which Listen of see whether there is either a Related Code with this Event.
Then there is also a method of upper interface Which Contains various Methods which contains Some Code and when an Event Occurs then which Code or what Statements will be Executed are decided by the Methods of this Event. By using this when a user clicks a buttons all the information about the event and its source identify by the event object. We can use any event in the java program will be represented by a particular event object so this object will be passed as argument to the method. The way in which events are handled in java using these methods and interfaces is referred to as the delegation event modal.
Process of Handling Events
The delegation model explains how event handling takes place in java. You can map the event delegation model to the actual Java statements that are used for handling events. For example, whn an end user click the AWT button, a message is displayed in the text event. The source in this case is the AWT button that that end user interacts with the event listener that receives and processes the button click event in the example is the ActionListener interface. The class you will create to handle this event should implement the ActionListener interface. The ActionListener interface defines a single method, actionPerformed() that you will need to define within the class. The syntax of the user-defined class to handle event is:
The above syntax shows a class that implements the ActionListener interface and defines the actionPerformed() method. You will need to register the ActionListener interface to the button after creating the AWT button. The addActionListener(reference) method is used for registering the ActionListener interface. The syntax of the user-defined class to register event is:
The above syntax shows how to use the addActionListener() method to register the AWT button, which is the source to the ActionListener interface, which is the event listener.
The following program code shows the complete program to display a message in the textfield when a button is clicked:
The above code will be stored in the file called EventExample.java, which on compilation will crate a class file called EventExample.class. Now compiple this file using below commands in the DOS mode:
C:\>jdk1.4\bin>javac EventExample.java
C:\>jdk1.4\bin>appletviewer EventExample.java
|
||
Download Complete Program |
Thanks for sharing this informative post.
ReplyDeleteJava Training in OMR
Join Now Java course , Online Java course
ReplyDeletehttps://corosocial.com/read-blog/141413_future-of-java-technology-will-help-you-get-there.html
APTRON provides the best Java course , Online Java course for beginners as well as advanced programmers. Java course, Online Java course will help you to learn and nourish your programming skills in Java.