16 Working with JavaBeans
This chapter describes Oracle JDeveloper support for JavaBeans technology.
This chapter includes the following sections:
16.1 About Working with JavaBeans
JavaBeans Component technology lets you implement your own framework for data retrieval, persistence, and manipulation of Java objects. You can use JavaBeans technology to create reusable software components for building Java applets and Java client applications. In a Java EE application, applets and application clients can communicate with business-tier components directly or indirectly through web-tier components. For example, a client running in a browser would communicate with the business tier through JSP pages or servlets.
Although JavaBeans components are not considered Java EE web components according to the Java EE specification, JavaBeans components are often used to handle data flow between server components and application clients or applets on the client tier, or between server components and a database on the back end.
For more information on JavaBeans, for example, the basic notion of JavaBeans, creating JavaBeans, and what makes a bean, see
http://download.oracle.com/javase/tutorial/javabeans/. The tutorial also contains lessons on writing a simple bean, bean properties, manipulating events and other topics.
16.2 Using JavaBeans in JDeveloper
JavaBeans are the Java building blocks used in the Swing GUI builder to build a program. Each JavaBean represents a program element, such as a user interface object, a data-aware control, or a system facility. You build your program by choosing and connecting these elements.
In order to speed up your UI design work in the future, create JavaBean components such as toolbars, status bars, checkbox groups, or dialog boxes that you can add to the Components window and reuse with no (or only minor) modifications
JavaBeans are objects in the true object-oriented programming (OOP) sense. Because they are true objects, JDeveloper components exhibit the following:
Encapsulation of some set of data and data-access functions.
Inheritance of data and behavior from a superclass.
Polymorphism, allowing them to operate interchangeably with other objects derived from a common superclass.
Each component encapsulates some element of a program, such as a window or dialog box, a field in a database, or a system timer. Visual components must ultimately extend either
java.awt.Component or extend some other class that derives from it such as
javax.swing.Panel. Non-visual JavaBeans components do not have this requirement.
To be recognized and used in JDeveloper, components must conform to the JavaBeans specification.
To be useful in a program, a JavaBean must provide the means by which it can be manipulated or interact with other components. JavaBeans meet this requirement by defining properties, methods, and events.
All components have properties, methods, and events built into them. Some of the properties, methods, and events that components provide are actually inherited from ancestor classes, which means they share these elements with other components. For example, all UI components inherit a property called
background that represents the background color of the component. Each component can also introduce its own unique properties, methods, and events. For example, the Swing Checkbox component has a property called
selected that indicates whether or not this component initially appears checked.
16.2.1 How to Implement an Event-Handling Method
In the GUI builder, you see an event primarily as the event-handling method that must be implemented in the class that contains the component. For example, suppose you have a button named
jButton1 in a container called
NewJFrame and you want something to happen when an end user clicks
To implement the event-handling method:
In the Properties window, expand the Events node. Possible events are listed, and
actionPerformedis the event generated when a button is pressed.
From the field next to
actionPerformedselect the default name of the handler,
JDeveloper switches to the
NewJFramesource view and inserts an event-handling method into
NewJFramethat is called when that event occurs.
The method is called
Add code into the method to respond to the button press.
The end user sees all of the potential events from
jButton1 listed on the Events page of the Properties window. As the component writer, you are responsible for creating the component class in such a way that all the events it generates will appear in the Properties window. All the end user must do to use your bean is write the code that fills in the event-handling method.
16.2.2 What Happens When You Create an Event-Handling Method
Behind the scenes, JDeveloper also generates additional code in the
Frame1.java file to handle the other aspects of event listening:
It generates an anonymous inner class for the action adapter that implements the
It instantiates the class in
It registers itself as a listener for the button1 event by calling
All of this code is visible in the source, but your primary task is to fill in the event-handling method that the action adapter calls when the event occurs.
16.3 Understanding Standard Event Adapters
a description of the Listener Generation Style property. It has its default value in Swing GUI Builder preferences where it is already described in the help. This default value is then used for newly created GUI forms. It can be changed then for each form separately: open GUI form, select its root node in Structure window and then in Code Generation properties set the Listener Generation Style property.
You can control how JDeveloper generates the adapter class by selecting the desired option from the Code Style page of the Project Properties dialog (for more information, see Section 4.3.11, "How to Set Properties for Individual Projects").
16.3.1 How to Create an Event Set
An event set defines a type of event, what it communicates, and what is required to generate and listen to the event. You can create a set of custom events and create an
EventListener interface and an
EventObject class to support those events. The event-listener interface describes the events of the event set.
To create an event set:
In the Applications window, select the project you wish the bean to be added to.
From the main menu, choose File > New from Gallery.
In the New Gallery, in the Categories tree, expand General and select Java.
In the Items list, double-click Event Set.
In the Create Event Set dialog, in the Name field, enter the name of the event set.
Add, edit, or remove events in the Notifications field.
Click OK to add the new event set classes to your project.
16.3.2 How to Make a Component Capable of Firing Events
When you develop a bean, you must think of all the events that the bean should be able to generate. The means by which components communicate with each other is called event passing. Components fire events. The event is then delivered to the components that are to be notified. The notified components can then perform some action based on the event that took place.
To make a component capable of firing events:
Determine what kind of event needs to be fired, and either:
Select an appropriate existing event set from the AWT or JFC, or
Create a new event set.
Create event registration methods for the component.
Create an event notification/propagation mechanism for the event:
Call the event that is fired and call the event notification mechanism from the key points in your bean where such an event should be sent.