In the context of JavaBeans, the communication between components through events is categorized into two main types:
- π§ Low-Level Events
- Low-level events correspond directly to user input or hardware events. These are primitive and raw in nature and relate closely to AWT (Abstract Window Toolkit) components.
β
Examples:
MouseEvent
(e.g., mouse clicks, movement)
KeyEvent
(e.g., key presses)
FocusEvent
, WindowEvent
, ComponentEvent
π Communication with Other Events:
- Low-level events may trigger semantic events or invoke other beansβ methods conditionally, acting as raw triggers for higher-level logic.
- Semantic Events
- Semantic events abstract away the low-level details and reflect user intent or logical actions.
β
Examples:
ActionEvent
(e.g., button click)
ItemEvent
(e.g., checkbox selected)
AdjustmentEvent
(e.g., scrollbar moved)
TextEvent
(e.g., text field change)
π‘ Usage in JavaBeans:
These are more commonly used in JavaBeans since they provide a cleaner abstraction for component interaction. For example, when a button is clicked, it fires an `ActionEvent` regardless of the physical input method.
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Logical action: Button clicked");
}
});
π Communication with Other Events:
π Integration Between the Two
Low-Level Event |
Triggers |
Semantic Event |
Triggers Bean Method |
MouseEvent |
β |
ActionEvent |
β doSomething() |
JavaBeans components listen for
semantic events because they offer a clean and consistent interface for component collaboration, while low-level events are used when fine-grained control or custom behavior is needed.
Events are a very important part of JavaBeans because they form the underlying mechanism by which Beans communicate. How can a Bean communicate using an event? Well, when an event occurs in a Bean the Bean sends an event notification to any application, applet, or other Bean interested in knowing about the event. So, if you click the mouse on a button Bean that is part of an e-mail application, for example, the Bean sends an event notification to the application. The application then processes the event and takes some kind of action, such as sending an e-mail message. In this example, the communication between the Bean and its parent application is carried out by the event. This type of scenario can exist between Beans and stand-alone applications, between Beans and applets, and even between two Beans. It is not hard to see that without events, and therefore without a means of communication, Beans would not be of much use.
Events form a fundamental part of the JavaBeans architecture in that they provide the means by which Beans communicate with applications and other Beans. JavaBeans inherits its event handling facility directly from the Java 1.1 delegation event model. JavaBeans, therefore, supports two different types of events:
low-level events and
semantic events[1].
Low-level events are events fired in response to a low-level input or visual user interface interaction. Examples of low-level events are mouse drags and key presses.
An event fired in response to a low-level input or visual user interface interaction.
Semantic events are events fired when an action occurs that is based on the semantics of a particular Bean.
An example of a
semantic event is a button press event, which is related to the specific function (semantics) of a button Bean. In the next lesson, you learn how event listeners are used to facilitate event communication.
- Javabeans Semantic Events
Following are the semantic events supported in the Java 1.1 API
Semantic Events |
Description |
ActionEvent | Fired when a generic action occurs. |
AdjustmentEvent | Fired when a value is adjusted. |
ItemEvent | Fired when an item state changes. |
[1]Semantic event: An event fired when an action occurs that is based on the semantics of a particular Bean.
