Fusion Middleware Documentation
Advanced Search


Developing Applications with Oracle JDeveloper
Close Window

Table of Contents

Show All | Collapse

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 jButton1.

To implement the event-handling method:

  1. Select jButton1 in the NewJFrame editor.

  2. In the Properties window, expand the Events node. Possible events are listed, and actionPerformed is the event generated when a button is pressed.

  3. From the field next to actionPerformed select the default name of the handler, jButton1ActionPerformed.

  4. JDeveloper switches to the NewJFrame source view and inserts an event-handling method into NewJFrame that is called when that event occurs.

    The method is called jButton1ActionPerformed() by default.

  5. 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 ActionListener interface.

  • It instantiates the class in Frame1.

  • It registers itself as a listener for the button1 event by calling button1.addActionListener().

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:

  1. In the Applications window, select the project you wish the bean to be added to.

  2. From the main menu, choose File > New from Gallery.

  3. In the New Gallery, in the Categories tree, expand General and select Java.

  4. In the Items list, double-click Event Set.

  5. In the Create Event Set dialog, in the Name field, enter the name of the event set.

  6. Add, edit, or remove events in the Notifications field.

  7. 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:

  1. 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.

  2. Create event registration methods for the component.

  3. Create an event notification/propagation mechanism for the event: fire<yourEventName>Event()

  4. 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.