- Event classes are stored in java.awt.event package
Event handling mechanism
⟶Delegation Event Model
- It is a modern approach.
- It uses the concept of source and listener.
- Source generates an event and sends it to one or more listeners.
- Listener must register with the source to receive the event notification.
→Now what is an event?
- An event is an object that describes state change in source.
- for example:- Suppose a checkbox is registered with the ItemListener , so whenever the checkbox is selected or de-selected the state of the checkbox changes, therefore whenever the checkbox changes it's state it' s listener is notified which in turn contains a method called itemStateChanged(ItemEvent ie) which has default parameter of ItemEvent object which in turn describes the event generated by the object.
→How to register a source with the listener
- A source is an object that generates an event if there is some change in the internal state of that object.
- To register a source with the listener addTypeListener(TypeListener object) method is used.
- for example :- addActionListener(this), addItemListener(this)... like this way you can register components with their specified listeners.
- Here the TypeListener specifies object of class implementing listener interface.
- for example :- To implementing ActionListener, addActionListener(new ActionListener(){public void actionPerformed(){}});
→Explanation of how an even occurs
- When an even occurs all the registered listeners are verified. this is known as multi-casting the event.
- Some sources may allow only one listener to register by using following method. addTypeListener(TypeListener obj)throws java.util.TooManyListenerException
- It is the type of uni-casting the listener.
- To un-register with a specific type of event listener, you can use the following method public void removeTypeListener(TypeListener object).
- At the root of java event class hierarchy we have EventObject which is in package java.util.EventObject
- EventObject class contains two methods getSource() and toString().
- AWTEvent class belongs to java.awt package and is a sub-class of EventObject class
- AWTEvent is a super class of all awt based events.
- It has a function getId() which is used to determine the type of event.
Summary
- EventObject is super class of all events
- AWT is super class of all awt based events
- java.awt.event package defines various events generated by various user interfaces.
0 comments:
Post a Comment