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:
An event of class ItemEvent is generated when the state of an item, such as a radio button or checkbox, is changed.
The itemStateChanged(ItemEvent event) method is called when the state is changed.
Implement the ItemListener interface to handle a ItemEvent.
Since there is only one method in the interface, there is no ItemAdaptor class.
Use the addItemListener() method to add the listener object to the component. (This registers the listener.)
Your handler should check if the state changed to selected or unselected:
if (e.getStateChange() == ItemEvent.SELECTED) or
if (e.getStateChange() == ItemEvent.DESELECTED)