JavaBeans events provide a standardized mechanism for communication between components in Java, typically within a GUI environment like Swing or in frameworks that rely on component interaction.
🔍 Purpose of JavaBeans Events
The primary purpose is to enable loosely coupled components to communicate, where one component (the event source) notifies other components (the event listeners) when certain actions occur.
⚙️ Function and Workflow
JavaBeans use the event delegation model, consisting of the following roles:
Role
Description
Event Source
The object that generates events (e.g., a button).
Event Object
A subclass of java.util.EventObject carrying event details.
Event Listener
An interface (e.g., ActionListener) that receives notifications.
Event Registration
A method like addActionListener() to register listeners with the source.
🧩 How It Works
User action occurs (e.g., a button click).
Event object is created (e.g., ActionEvent).
Source fires event using a method like fireActionPerformed().
Registered listeners receive the event and handle it using callback methods (e.g., actionPerformed()).
✅ Example
public class MyButton extends java.awt.Button {
public MyButton() {
this.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button was clicked!");
}
});
}
}
📌 Key Advantages
Promotes modularity.
Allows for reusable components.
Supports event-driven programming, especially in GUI and framework-based applications.
JavaBeans Events are a Mechanism for Communication between Objects
JavaBeans events are a mechanism for communication between objects in a Java program. They allow objects to notify other objects when a specific event occurs, such as a button being clicked or a text field being edited. JavaBeans events are based on the observer pattern, which is a design pattern that defines a one-to-many dependency between objects. In this pattern, an object (the subject) maintains a list of its dependents (the observers). When the subject's state changes, it notifies all of its observers, which can then update their own state accordingly.
In JavaBeans, the subject is called the event source, and the observers are called event listeners. The event source is the object that generates the event, and the event listener is the object that receives the event and responds to it.
JavaBeans events are used to communicate a wide variety of information between objects. For example, a button might generate an event when it is clicked, a text field might generate an event when its text is changed, and a menu might generate an event when an item is selected.
Roles of Event Listeners, Event Sources, and Event Adapters:
Event Listeners: Event listeners are objects that implement an interface that defines the methods that will be called when an event occurs. For example, a button might have an event listener that implements the ActionListener interface, which defines the actionPerformed method. When the button is clicked, the actionPerformed method will be called.
Event Sources: Event sources are objects that generate events. They maintain a list of event listeners and notify them when an event occurs. For example, a button is an event source that generates an ActionEvent when it is clicked.
Event Adapters Event adapters are objects that adapt one event type to another. This can be useful when you want to use an event listener that is designed for a different type of event. For example, you might use an event adapter to adapt a MouseEvent to an ActionEvent so that you can use the same event listener for both types of events.:
JavaBeans events are a powerful mechanism for communication between objects in a Java program. They can be used to create a wide variety of applications, including graphical user interfaces (GUIs), web applications, and enterprise applications.
Module learning objectives
This mdoule will discuss event listeners and event sources, as well as event adapters. You will also learn how events are delivered.
Toward the end of the module you will use the BeanBox to connect Beans together with events. After completing the module, you will have the knowledge and skills necessary to:
Register event listeners to receive Bean events
Handle Bean events broadcast to event listeners
Use the BeanBox to connect Beans together with events
The JEE platform defines JavaBeans for component architectures
Conventions for how software components interact
Some built-in Java classes
Example Component Architecture
The diagram illustrates Model 1 Architecture in early Java web application development, often associated with JavaServer Pages (JSP) and Servlets. Here's a breakdown of how it works based on the diagram:
Clients (Users)
There are two types of interactions shown:
One client interacts with a Servlet.
Another interacts directly with a JSP.
Servlet-Based Interaction
The client sends a GET request (e.g., requesting a form).
The Servlet sends back the form.
The client then submits the form via a POST or GET, which again reaches the Servlet.
The Servlet handles the request and interacts with an I/O component, which connects to a file store for reading/writing data.
JSP-Based Interaction
A different client makes a GET request for data.
The JSP directly accesses the I/O module to retrieve data from the file store and returns it to the client.
File Store
Acts as the persistent storage system (e.g., database or flat files).
Accessed by both Servlet and JSP via an I/O layer.
Characteristics of Model 1 Architecture:
Presentation logic and business logic are mixed: In Model 1, JSPs often include both HTML and embedded Java code, violating separation of concerns.
Tight coupling: The components (Servlet/JSP) handle both control flow and view rendering.
Not scalable: As the application grows, this architecture becomes harder to maintain.
Suitable for small, simple applications.
The image depicts Model 2 Architecture, also known as the MVC (Model-View-Controller) design pattern for Java web applications. This architecture separates concerns more cleanly than Model 1, making it better suited for larger, maintainable web applications.
Component Roles in Model 2 (MVC) Architecture:
Client
The user interacts with the web application by sending requests.
Initially requests the Form JSP or submits data that goes to the Servlet.
Controller (Servlet)
Acts as the Controller in MVC.
Handles incoming requests from the client (e.g., form submissions).
Coordinates the flow of data between the JSPs and the JavaBean.
Uses forward to dispatch requests to the appropriate JSPs (e.g., Form JSP or Present JSP).
View (Form JSP and Present JSP)
Form JSP: Displays a form for user input.
Present JSP: Displays results or processed data to the user.
These JSPs do not contain business logic, only presentation logic (HTML/CSS).
Model (JavaBean and I/O)
The JavaBean acts as the Model, encapsulating business logic and application data.
It interacts with the file store via the I/O layer for reading/writing persistent data.
Data retrieved or modified by the JavaBean is passed back to the JSP via the controller.
File Store
The backend storage, accessed by the I/O layer in the Model (JavaBean).
Key Characteristics of Model 2 Architecture:
Aspect
Description
Separation of Concerns
Logic is clearly divided: View (JSP), Controller (Servlet), Model (JavaBean)
Scalability
Easier to maintain and extend large applications
Reusability
JavaBeans (Model) can be reused across different views
Maintainability
Logic isolation enables better testing and easier updates
Flow Control
Servlet manages application flow and delegates rendering to JSPs
Flow Summary:
Client sends request (e.g., for form or to submit data).
Servlet processes the request.
Servlet invokes JavaBean to interact with data storage (Model).
Servlet forwards the result to Present JSP for display.
Client receives the rendered HTML from Present JSP.
In the next lesson, you learn about two event types: low-level and semantic.