17

JClass BWT and Java Beans

Java Beans · Properties

New Event Model · Component Classes

Java Beans

The new Java Development Kit ( JDK) 1.1 provides a number of new features and improvements over the previous release. In particular, it includes the ability to create "Bean" components.

Java Beans is a specification that calls for the design of reusable, pre-built Java software components. It is designed to be a fully platform-independent component model written for the Java programming language. It enables developers to write components that can be re-used in other applications, reducing the total time needed to write complete applications. JClass BWT components are Java Bean-compliant.

The three main characteristics of a Bean are:

Properties are the named set and get public attributes associated with a Bean that can be read/write, read-only or write-only. A Bean also allows its methods to be called from other components. Beans fire events to notify other components that an action has happened.

Properties

Under the new model, "properties" are the named method attributes of a class that can affect its appearance or its behavior. Properties that are readable have a "get" method which enables the developer to read a property's value, and those properties which are writable have a "set" method which enable a property's value to be changed.

For example, the JCOutliner object in JClass BWT has a property called NodeIndent. This property sets the indentation of each node relative to its parent in pixels. To set the property value, the setNodeIndent() method is used. To get the property value, the getNodeIndent() method is used.

The main advantage of following the Java Beans specification is that it makes it very easy for a Java Integrated Development Environment (IDE) to "discover" the set of properties belonging to an object. The developer can then manipulate the properties of the object easily through the graphical interface of the IDE when constructing a program.

There are three ways in which JClass BWT properties can be modified:

In JClass BWT, all three methods use the same property name. As a result, this manual describes the behavior of a particular property instead of the behavior of a particular method or HTML tag.

Setting Properties with a Java IDE at Design-Time

JClass BWT can be used with a Java Integrated Development Environment (IDE), and its properties can be manipulated at design time. Consult the IDE documentation for details on how to load third-party Bean components into the IDE.

Most IDEs list a component's properties in a property sheet or dialog. Simply find the property you want to set in this list and edit its value. Again, consult the IDE documentation for complete details.

For detailed information on which properties are available from within a component, consult the JClass BWT API Reference.

Setting Properties using Methods

As mentioned previously in this chapter, every property in JClass BWT has a set and get method associated with it. For example, to retrieve the value of the NodeIndent property of the JCOutliner instance "myoutliner":

 
	myoutliner.getNodeIndent()

To set the NodeIndent property in the same object:

 
	myoutliner.setNodeIndent(24);

Setting Applet Properties in an HTML File

Another way to set BWT applet properties is through an HTML file. Applets built with JClass BWT automatically parse applet <PARAM> tags and set the component properties defined in the file.

Using HTML to set properties has the following benefits:

BWT component properties are coded in HTML as applet <PARAM> tags. The NAME element of the <PARAM> tag specifies the property name; the VALUE element specifies the property value to set.

Determining Which Technique to Use

JClass offers a lot of choice when it comes to modifying properties. The choice of technique is a personal preference, however, keep in mind the following points when deciding which technique to use:

New Event Model

Events are a mechanism used to propagate state change notifications between a source object and one or more target listener objects. Events are typically used within windowing toolkits for delivering notifications of such things as mouse or keyboard actions, or other programmatically-defined actions.

In the JDK 1.0.2, the behavior of Java programs was typically modified by subclassing and overriding event handling classes. Subclassing overrode the default behavior of a method. This approach was clumsy because it required the programmer to have a thorough understanding of base class methods and internals before writing any code.

Bean-compliant JClass programs (like JClass BWT) provide the means for an application to be notified when an event occurs through event listeners. It works like this: if a component is acted upon by the user or from within the program, a JCFooEvent is fired. The JCFooListener (which has been registered by calling addFooListener() on the component) receives the instance and enacts the action to be taken. The programmer uses the JCFooListener to define what action or actions should take place when it receives the JCFooEvent.

Component Classes

IDE users will note the existence of several "component" versions of classes like JCList (for example, JCListComponent) and JCOutliner (for example, JCOutlinerComponent).

Component versions exists because the current generation of Java IDEs do not support properties in contained objects. While current Java IDEs allow properties in contained objects to be modified, they cannot yet properly generate code for the property change. Class files like JCOutlinerComponent work around this problem by exposing many JClass BWT properties in one object. While not all properties are provided, the most common properties are available.

Since the component class is a subclass, it is possible to switch and use the contained properties in its parent.