Java Reference
In-Depth Information
Figure 2-2. Event delegation sequence diagram
Event Listeners As Observers
Using event listeners to handle an event is a three-step process:
1.
Define a class that implements the appropriate listener interface (this includes
providing implementations for all the methods of the interface).
2.
Create an instance of this listener.
3.
Register this listener to the component whose events you're interested in.
Let's take a look at the three specific steps for creating a simple button that responds to
selection by printing a message.
Defining the Listener
To set up event handling for a selectable button, you need to create an ActionListener , because
the JButton generates ActionEvent objects when selected.
class AnActionListener implements ActionListener {
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("I was selected.");
}
}
Search WWH ::




Custom Search