Events and Listeners Flashcards
What method should a class that is the source of methods contain
A method that allows for registration of listeners - one of reach listener event type
By convention the methods look like:
public void addEventCategoryListener (EventCategoryListener this)
What is an event
What is contained in event classes
A state change
Event classes contain methods that can be used to implement the interface
Each event has corresponding listener interface
Explain the relationship between event listener interfaces, event notification methods and event classes
Event listeners (e.g. KeyListener) consist of multiple event notification methods (e.g. keyPressed, keyReleased). This is because when an event listener is notified, it deals with the event via these methods.
Event notification methods categorized into event classes (e.g. KeyEvent)
Event classes are directly linked to an event listener interface
Describe the java event framework (structure)
One abstract class => EventObject
An interface => EventListener
Explain the structure of the delegation model
SOURCE
Event generated at this level, source must register listeners.
Event generation occurs when there is some change to the internal state of the source
LISTENER
An object that is notified that an event has occurred
Registered with the source of an event
Methods of listener object deals with event when notified
EVENT
An object that contains information about a state change in a source
When should you use events and listeners in your program
Any situation where you’re waiting for something in you program to happen but you can’t make it happen
Why should we use event-driven programming when implementing GUI’s
GUI’s have multiple components that can be interacted with
Programs need to react to all these different events
When implementing IDEs, we need to implement functionality and understand events
What is event-driven programming
Give one advantage
Give one disadvantage
Resources are released when notified that an event has occurred
Better use of resources
More complex to implement
What is a polling event
Give one advantage
Give one disadvantage
When you keep checking for the events’ occurrence, best way to implement this is by using a while loop
Easy to implement
Wasteful of resources