Swing - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Swing Events

Swing Events

shape Description

Swing Events can be described as a particular point of an instance or behavior of that instance.For instance, clicking on a Button, entering characters on the keyboard. Here Swing Events are divided into two types such as Foreground Events and Background Events. 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.

Event

shape Example

Below is the source code to show how the event will perform in the application. AEvent.java [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(); } } [/java] Create a class that should extend from Frame and should implement ActionListener. Create constructor that should be same as class name. [java]class AEvent extends Frame implements ActionListener{ TextField tf; AEvent()[/java] Create text field, button and add ActionListener to this button. [java]tf=new TextField(); Button b=new Button("click me"); b.addActionListener(this); [/java] Action should perform on the textfield then it displays output as SPlessons. [java]public void actionPerformed(ActionEvent e){ tf.setText("SPlessons"); } [/java] Output: Output will be as follows. When  click on the click button,"Welcome to SPlessons" will be displayed in the text field.

Swing Event Classes

shape Description

Swing event classes play a major role in Swing applications and indicates the event. While developing an application based on the Swing technology developer can use more Swing classes depends on the requirement because Swing consists of more classes so it is a little bit hard to use all the classes in an application. Splessons will teach about commonly used classes. EventObject class is the base class and it can derive the exact behavior and state of the instance (object). It's available in the Java. util package.

shape Syntax

public class EventObject extends Object implements Serializable
Protected Object Source is the field of Java. util. EventObject, and on which only event will be happening initially. This class consists of methods also and these methods will be inherited from the Java. util. object.

Event Classes

shape Description

It is the base of the AWTEvent and it can be derived from the java.awt package. The declaration of the java.awt.AWTEvent can be indicated as follows. [java]public class AWTEvent extends EventObject[/java]

Fields of AWTEvent

shape Description

Constructors of AWTEvent

shape Description

Methods of AWTEvent

shape Description

Above methods will be inherited from the package such as java.lang.object

shape Description

When user click on the button it will be performed.The declaration of the java.awt.event.ActionEvent class can be shown as follows. [java]public class ActionEvent extends AWTEvent[/java]

Fields of ActionEvent

shape Description

Methods of ActionEvent

shape Description

Above methods will be inherited from the package such as java.awt.AWTEvent and java.lang.Object

shape Description

The instance of WindowEvent will be activated when window is open or closed or terminated.The package of this class can be declared as java.awt.event.WindowEvent. The declaration of java.awt.event.WindowEvent can be written as follows. [java]public class WindowEvent extends ComponentEvent[/java]

Fields of WindowEvent

shape Description

Methods of ActionEvent

shape Description

The above methods will be inherited from the java.lang.object, java.awt.AWTEvent.

shape Description

This event will be useful when mouse is dragged or moved. This class will be available in java.awt.event.MouseMotionEvent. The declaration of this class can be written as follows. [java]public class MouseMotionEvent extends InputEvent[/java]

Methods of MouseMotionEvent

shape Description

The above all the methods inherited from java.awt.event.ComponentEvent, java.awt.event.InputEvent.

shape Description

This class will be use full to update or paint the methods and it will be available in the package of java.awt.event and can be declare as java.awt.event.PaintEvent.The declaration can be shown as follows. [java]public class PaintEvent extends ComponentEvent[/java]

Fields of PaintEvent

shape Description

Methods of PaintEvent

shape Description

The above methods will be inherited from the java.awt.ComponentEvent, java.lang.Objectjava.awt.AWTEvent.

Summary

shape Key Points

  • Swing Events - Listener interface should be registered with listener class.
  • Swing Events - Event source, Event listeners, Event object are the participants of the Delegation Event Model.
  • Swing Events - java.io.Serializable interface needs to be implemented by the class.
  • Swing Events - int getID() is a method of AWTEvent i.e indicates the type of the event.