JavaBean Events   «Prev  Next»
Lesson 2Event types
Objective Learn about low-level and semantic events.

Two Event types in JavaBeans 1) low-level 2) Semantic

In the context of JavaBeans, the communication between components through events is categorized into two main types:
  1. πŸ”§ 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
      πŸ“‘ Usage in JavaBeans:
    • These events are generated by GUI components and can be used by JavaBeans to detect basic user interactions.

    • button.addMouseListener(new MouseAdapter() {
      Β  Β  public void mouseClicked(MouseEvent e) {
      Β  Β  Β  Β  System.out.println("Mouse clicked on button");
      Β  Β  }
      });
              
      πŸ”„ 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.
  2. 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.


low-level-event1
Low Level Event

Communicating with Events - JavaBeans

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.

Low-level Events and JavaBeans

Low-level events
Following are the low-level events supported in the Java 1.1 API:
Low-level Event Description
ComponentEvent Fired when a Bean is resized, moved, hidden or shown.
FocusEvent Fired when a Bean receives or loses focus.
InputEvent Never actually fired; it serves as an organizational class for input (keyboard and mouse) events.
KeyEvent Fired when a Bean receives a key press or key release.
MouseEvent Fired when a Bean receives a mouse button click, mouse button release, mouse move, or mouse drag.

Low-level event

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
    ActionEventFired when a generic action occurs.
    AdjustmentEventFired when a value is adjusted.
    ItemEventFired 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.

SEMrush Software