UNIT III Event Handling
UNIT III Event Handling
EVENT HANDLING
Unit Outcomes (UOs): -
Total Marks=12
UNIT-III EVENT HANDING
The Delegation Event Model:
• The modern approach to handling events is based on the delegation event model,
which defines standard and consistent mechanisms to generate and process events.
Concept is quite simple: a source generates an event and sends it to one or more
listeners. In this scheme, the listener simply waits until it receives an event. Once
received, the listener processes the event and then returns.
In the delegation event model, listeners must register with a source in order to
receive an event notification. This provides an important benefit: notifications are sent
only to listeners that want to receive them.
The Delegation Event Model:
Events
In the delegation model, an event is an object that describes a state change in a source.
It can be generated as a consequence of a person interacting with the elements in a
graphical user interface.
Some of the activities that cause events to be generated are pressing a button, entering a
character via the keyboard, selecting an item in a list, and clicking the mouse. Many other
user operations could also be cited as examples.
Events may also occur that are not directly caused by interactions with a user interface. For
example, an event may be generated when a timer expires, a counter exceeds a value, a
software or hardware failure occurs, or an operation is completed. You are free to define
events that are appropriate for your application.
PART-II [unit-III]
Advance JAVA Programming (22517)
Computer Engineering / IT Program Group (CO/CM/IF/CW)
Here, Type is the name of the event and el is a reference to the event listener. For example,
the method that registers a keyboard event listener is called addKeyListener( ).
The method that registers a mouse motion listener is called addMouseMotionListener( ).
Here, Type is the name of the event and el is a reference to the event listener. For example, to
remove a keyboard listener, you would call removeKeyListener( ).
The methods that add or remove listeners are provided by the source that generates events.
For example, the Component class provides methods to add and remove keyboard and
mouse event listeners.
Event Sources
An event source is the object that generates the event. For example if you click a
button an ActionEvent object is generated.
The object of the ActionEvent class has all corresponding information about this
event. Here button is an event source.
Various event sources can be button, checkbox, textbox, scrollbars and so on.
Event Listeners
When an event occurs, first of all an event object of the appropriate type is created.
This object is then passed to a Listener.
A listener must implement the interface that has the method for event handling.
The java.awt.event package contains definitions of all event classes and listener
interface.
An Interface contains constant values and method declaration.
The methods in an interface are only declared and not implemented, i.e. the methods do
not have a body.
The interfaces are used to define behaviour on occurrence of event that can be
implemented by any class anywhere in the class hierarchy.
Event Classes
Event classes are the classes responsible for handling events in the event handling
mechanism.
The EventObject class is at the top of the event class hierarchy. It belongs to the
java.util package. And other event classes are present in java.awt.event package.
The getSource() and toString() are the methods of the EventObject class.
There is getId() method belonging to java.awt.event
For example, if a keyboard event occurs, you can find out whether the event was key
press or key release from the event object.
java.awt.event
PART-III [unit-III]
Advance JAVA Programming (22517)
Computer Engineering / IT Program Group (CO/CM/IF/CW)
Manjula Athani
HOD - Computer Engineering
Email: [email protected]
ActionEvent Class
Constructors ActionEvent(Object source, int id, string command, long when, int
modifier) The source indicates the object due to which the event is
generated.
The id which is used to identify the type of event.
The command is a string that specifies the command that is associated with
the event.
The when denotes the time of event.
The modifier indicates the modifier keys such as ALT, CNTRL, SHIFT that
are pressed when an event occurs.
Constants There are four constants that are used to indicate the modifier keys being
pressed. These constants are CTRL_MASK, SHIFT_MASK, META_MASK
and ALT_MASK.
ItemEvent Class
Constants 1) ITEM_FIRST : Marks the first integer id for the range of item
The code represents the virtual keycode such as VK_UP, VK_ESCAPE and
so on
Description This event occurs when mouse action occurs in a component. It reacts for both
mouse event and mouse motion event.
Mouse Events
Fields TEXT_FIRST : The first number in the range of ids used for text
events.
TEXT_LAST : The last number in the range of ids used for text
events.
TEXT_VALUE_CHANGED : This event id indicates that
object's text
changed.
WindowEvent Class
Description This is an event that indicates that a window has changed its status.
Description This is an event that indicates that a window has changed its status.
1) The keyPressed() event is for handling the event of pressing a key, similarly
keyReleased() event is for handling the event of release of key.
2) Using keyTyped() method we can display the character typed on the screen. The
only needed method for this is getKeyChar() which returns the currently typed
character.
PART-V [unit-III]
Advance JAVA Programming (22517)
Computer Engineering / IT Program Group (CO/CM/IF/CW)
When an event occurs, first of all an event object of the appropriate type is created. This object is then passed to a
Listener.
A listener must implement the interface that has the method for event handling.
The java.awt.event package contains definitions of all event classes and listener interface.
An Interface contains constant values and method declaration.
The methods in an interface are only declared and not implemented, i.e. the methods do not have a body.
The interfaces are used to define behavior on occurrence of event that can be implemented by any class anywhere in the
class hierarchy.
ActionListener Interface
This interface defines the method actionPerformed() which is invoked when an ActionEvent occurs.
The syntax of this method is
This interface is used when an item from a list is selected. For example a choice is made or
if checkbox is selected.
The itemStateChanged() is the only method defined by the ItemListener interface.
The syntax is
This interface is defining the events such as keyPressed(), keyReleased() and keyTyped()
are used.
These methods are useful for key press, key release and when you type some characters.
void keyPressed(keyEvent k)
void keyReleased(keyEvent k)
void keyTyped(keyEvent k)
MouseListener Interface
This interface defines five important methods for various activities such as mouse click, press, released,
entered or exited. These are
void mouseClicked(MouseEvent m)
void mousePressed(MouseEvent m)
void mouseReleased(MouseEvent m)
void mouseEntered(MouseEvent m)
void mouseExited(MouseEvent m)
For handling mouse drag and mouse move events the required methods are defined by
MouseMotionListener interface. These methods are
void mouseDragged(MouseEvent m)
void mouseMoved(MouseEvent m)
TextListener Interface
An event of type TextEvent is generated when a value in textfield or textarea is entered
or edited.
The methods used by TextListener Interface are
public String paramString():
There are seven methods in which are related to windows activation and deactivation.
void windowOpened(WindowEvent w)
void windowClosed(WindowEvent w)
void windowClosing(WindowEvent w)
void windowActivated(WindowEvent w)
void windowDeactivated(WindowEvent w)
void windowIconified(WindowEvent w)
void windowDeiconified(WindowEvent w)
PART-VI [unit-III]
Advance JAVA Programming (22517)
Computer Engineering / IT Program Group (CO/CM/IF/CW)
∙ It is basically a class in Java that implements an interface with a set of dummy methods.
∙ The famous adapter classes in Java API are WindowAdapter, ComponentAdapter,
ContainerAdapter, FocusAdapter, KeyAdapter, MouseAdapter and
MouseMotionAdapter.
∙ Whenever your class implements such interface, you have to implements all of the seven
methods.
∙ WindowAdapter class implements WindowListener interface and make seven empty
implementation.
Inner classes:
Java inner class is a class within class is called inner class or interface.
Without existing one type of object there is no chance of existing another type of object then we should go for
inner class.
Example: University consists of several departments without existing university there is no chance of
existing department hence we have to declare department class inside university class.
}
}
NOTE:WITHOUT EXISTING OUTER CLASS OBJECT THERE IS NO CHANCE OF EXISTING INNER CLASS OBJECT
.
Based on position of declaration and behavior all Inner lasses are devided into 4 types.
1.Normal inner class
2.Method Local inner class
3.Anonymous inner class
4.Static nested class
1.Normal inner class: If you are declaring any named class directly inside a class
without static modifiers such type of inner class is called normal inner class.
Class Outer
{
Class Inner
{
}
}
Internal working of Java member inner class
The java compiler creates two class files in case of inner class. The class file name of inner
class is "Outer$Inner". If you want to instantiate inner class, you must have to create the
instance of outer class. In such case, instance of inner class is created inside the instance of
outer class.
The main purpose of anonymous inner class is just use of one time usage.
Java anonymous inner class example using class
abstract class Person{
abstract void eat();
}
class TestAnonymousInner
{
public static void main(String args[])
{
Person p=new Person()
{
void eat()
{
System.out.println("nice fruits");}
};
p.eat();
}
}
Java static nested class
A static class i.e. created inside a class is called static nested class in java. It cannot access non-static data members
and methods. It can be accessed by outer class name.
It can access static data members of outer class including private.
Static nested class cannot access non-static (instance) data member or method.
Java static nested class example with instance method
class TestOuter1
{
static int data=30;
static class Inner{
void msg()
{
System.out.println("data is "+data);}
}
public static void main(String args[])
{
TestOuter1.Inner obj=new TestOuter1.Inner();
obj.msg();
}
}
Output:
data is 30