The functionality of Event Handling is what is the further step if an action performed. Java foundation introduced “Delegation Event Model”i.e describes how to generate and control the events. The key elements of the Delegation Event Model are as source and listeners. Source is an instance and supplies data about to happen event to the event handler. The listener is also an instance and it gives the return alert in the event after the event has been received. Before this process listener should have registered on source for the purpose of alert notifications. Delegation event model overcomes the problems of hierarchical models.
Before constructing the listener class, a developer should have the listener interface which consists of callback methods.Callback methods should be implemented by listener class.
AEvent.java
package swing; import java.awt.*; import java.awt.event.*; class AEvent extends Frame implements ActionListener{ TextField tf; AEvent(){ tf=new TextField(); //creating textfield tf.setBounds(60,50,170,20); //creating bounds to the frame Button b=new Button("click me"); //if user click alerts will be passed b.setBounds(100,120,80,30); //creating bounds to the button box b.addActionListener(this); //after clicking on button it will be implemented add(b);add(tf); setSize(300,300); //creating size setLayout(null); setVisible(true); //it should always be as true setBackground(Color.red); //it depends on user requirements } public void actionPerformed(ActionEvent e){ tf.setText("SPlessons"); } public static void main(String args[]){ //here writing main method new AEvent(); } }
Create a class that should extend from Frame and should implement ActionListener. Create constructor that should be same as class name.
class AEvent extends Frame implements ActionListener{ TextField tf; AEvent()
Create text field, button and add ActionListener to this button.
tf=new TextField(); Button b=new Button("click me"); b.addActionListener(this);
Action should perform on the textfield then it displays output as SPlessons.
public void actionPerformed(ActionEvent e){ tf.setText("SPlessons"); }
Output:
Output will be as follows. When  click on the click button,”Welcome to SPlessons” will be displayed in the text field.
java.awt.AWTEvent
can be indicated as follows.
public class AWTEvent extends EventObject
Above methods will be inherited from the package such as java.lang.object
java.awt.event.ActionEvent
class can be shown as follows.
public class ActionEvent extends AWTEvent
Above methods will be inherited from the package such as java.awt.AWTEvent
and java.lang.Object
java.awt.event.WindowEvent
.
The declaration of java.awt.event.WindowEvent can be written as follows.
public class WindowEvent extends ComponentEvent
The above methods will be inherited from the java.lang.object
, java.awt.AWTEvent
.
java.awt.event.MouseMotionEvent
. The declaration of this class can be written as follows.
public class MouseMotionEvent extends InputEvent
The above all the methods inherited from java.awt.event.ComponentEvent
, java.awt.event.InputEvent
.
java.awt.event.PaintEvent
.The declaration can be shown as follows.
public class PaintEvent extends ComponentEvent
The above methods will be inherited from the java.awt.ComponentEvent
, java.lang.Object
, java.awt.AWTEvent
.