Lesson 6 | Implementing an event listener |
Objective | Learn how to implement event listeners. |
Implementing JavaBean Event Listener compared with JavaFX
No, JavaFX event listeners are not implemented in the same manner as event listeners in Java AWT 1.1. JavaFX event listeners are implemented using a different set of APIs and have a number of different features.
Here are some of the key differences between JavaFX event listeners and Java AWT 1.1 event listeners:
- JavaFX event listeners are implemented using the `javafx.event` package, while Java AWT 1.1 event listeners are implemented using the
java.awt.event
package.
- JavaFX event listeners are implemented using interfaces, while Java AWT 1.1 event listeners are implemented using abstract classes.
- JavaFX event listeners are more flexible and extensible than Java AWT 1.1 event listeners.
- JavaFX event listeners support event delegation, while Java AWT 1.1 event listeners do not.
Here is an example of a simple JavaFX event listener:
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
public class MyEventListener implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
// Do something when the event occurs.
}
}
To use this event listener, you would first create an instance of the `MyEventListener` class. Then, you would register the event listener with the control that you want to listen for events on. For example, to register the event listener with a button, you would use the following code:
button.setOnAction(new MyEventListener());
Once the event listener is registered, the `handle()` method will be called whenever the event occurs.
JavaFX event listeners are a powerful and flexible way to handle events in your GUI applications.
They offer a number of advantages over Java AWT 1.1 event listeners, such as flexibility, extensibility, and event delegation.
In the legacy version of the Java 1.1 event model, after registering an application as an event listener, you must implement the appropriate event listener interface. The addMouseListener()
method requires that the object passed in implement the MouseListener
interface. This is accomplished in the application class definition like this:
class MyApp extends Frame implements MouseListener {
...
}
The application then must implement all the event response methods defined in the MouseListener
interface. These methods follow:
mouseClicked()
mouseEntered()
mouseExited()
mousePressed()
mouseReleased()
Following is an example of how the application would implement one of these methods:
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse clicked!");
}
Method Explanation
This method prints a message to standard output any time the user clicks the mouse button while the mouse is over the Bean. Unfortunately, since interfaces must be fully implemented in order for a class to be instantiable, the application must implement all the event response methods in the MouseListener
interface even if it doesn't need them all. This is a problem inherent in this approach to handling events.
In the next lesson, you learn how event adapters are a cleaner approach to event handling.