A program that waits for events is called an event-driven program.
Let's look at the RunLight program from HW4 for a simple example.
Whiteboard Picture of Event Handling in RunLight.
What are Events?
- An event can be a mouse click, keyboard input, or other changes in environment.
- A well-design program will use the
Delegation Event Model (more in a bit)
- Each event uses a different class.
- The java.util.EventObject class is the parent class for all event classes.
- The java.awt.AWTEvent class is the parent class for all AWT events.
- There are 4 basics parts to event handling:
- An object that generates events (in the case of a GUI, a Component) keeps a list of listener objects.
- Each listener object needs to be added to the Component's "list of listeners". This is done by registering the listener to listen for a particular type of event.
- When the event is generated, the listener is notified with a call to a specific method in the listener, and the event is passed in as a argument.
- The method in the listener object calls a method belonging to the UI so that the UI implements the behavior that is tied to the event. Note: this method is called a Callback.
- Your event handlers will either implement the Listener or extend the Adaptor.
- Your event handlers will either be inner classes of the GUI, or be pass a reference to the GUI on construction (is this weak or strong association?).
- In general, if designed properly, the listener class will have little to no application logic written in the methods.
Event Types
Action Events:
For Text Components:
- An event of class ActionEvent is generated when the user edits the text in a text field and presses Enter (this indicates that they have finished editing.)
- The actionPerformed(ActionEvent event) method is called when the Enter key is pressed.
- Implement the ActionListener interface to handle an ActionEvent.
- Since there is only one method in the interface, there is no ActionAdapter class.
- Use the addActionListener() method to add the listener object to the text component.
For Buttons:
- An event of class ActionEvent is generated when the user clicks on the button.
- The actionPerformed(ActionEvent event) method is called when the button is clicked.
- Implement the ActionListener interface to handle an ActionEvent.
- Since there is only one method in the interface, there is no ActionAdapter class.
- Use the addActionListener() method to add the listener object to the button component.
Text Events:
- An event of class TextEvent is generated when the user changes the text in a text component.
- The textValueChanged(TextEvent event) method is called when the user changes the text.
- Implement the TextListener interface to handle a TextEvent.
- Since there is only one method in the interface, there is no TextAdaptor class.
- Use the addTextListener() method to add the listener object to the text component. (This registers the listener.)
Item Events:
More examples here: downloads/swing
Last Updated - 09/06/2020 01:57:26