[Contents] [Previous] [Next] [Index]

## Chapter 3
Using Java Objects as Components

This document describes how to use Java objects as Netscape ONE components when building applications in Visual JavaScript (VJS). You import Java objects into the VJS palette as components, and use the components to build applications.

Java objects that can be used as components in VJS include:

- Existing Java applets
- Applets implemented as Java Beans
- Java objects created using the JavaScript new operator (i.e., LiveConnect objects)

Sections in this document include the following topics:

- Using Java Objects
- Using Existing Applets
- Creating and Using Java Beans
- Using the Built-In Database Components

## Using Java Objects

Each type of Java object has advantages for incorporation into a VJS application. For example, the advantage to using an existing Java applet is that many such applets can be incorporated into VJS applications without modification.

The advantage to using an applet implemented as a Java Bean is that it includes a BeanInfo that describes the applet's properties, methods, and events. VJS makes use of this information to display information about the object at design time. If an applet does not provide optional BeanInfo information, VJS supports automatic introspection to determine metadata information about the applet. Providing this information enables developers to integrate the Java object into applications more easily.

Finally, for components without user interfaces, the JavaScript new operator provides a way to use components implemented in Java, including Java Beans, that are described using JavaScript Bean (JSB) files. Creating a JavaScript component using JSB files is described in Creating JavaScript Components. The JSB file structure and the syntax of its tags is described in JSB File Structure Reference.

## Using Existing Applets

Applets written with JDK 1.1 can be used without modification in VJS. An applet can be embedded in a VJS page either by:

- Importing pages from an existing web site.

- Creating an applet tag manually.

### Importing Pages

Choose File | Import to import an applet on an existing web site page into VJS. The page can be local or remote. VJS reads the HTML page and adds any directly referenced files to the current VJS project file. Files added to the project include the .CLASS file specified in the CODE attribute of the <APPLET> tags that appear on the page.

> NOTE: If an applet depends on other .CLASS or support files that are not directly referenced on the HTML page, these files are not automatically imported. You must repeat the import command for each of these extra files, and you must set the <APPLET> tag's FILEREFS attribute to list these files. FILEREFS is a quoted string consisting of a comma-delimited list of files. The list of files is used by VJS to ensure that as the applet component is used in applications and deployed, its support files are also used and deployed.

### Creating an Applet Tag by Hand

Instead of importing a page containing an applet into a VJS project, you can create an applet tag by hand. In this case, you must import the applet's .CLASS file into the project. If the applet also depends on other .CLASS or support files, you must repeat the import command for each of these extra files, and you must set the <APPLET> tag's FILEREFS attribute to list these files. FILEREFS is a quoted string consisting of a comma-delimited list of files. The list of files is used by VJS to ensure that as the applet component is used in applications and deployed, its support files are also used and deployed.

## Creating and Using Java Beans

Java Beans are Java classes written in accordance with the Sun Microsystems Java Beans specification. This section explains how to incorporate a Java Bean component into a VJS project, but it does not describe the actual implementation of a Java Bean component. The Java Beans specification can be found at http://splash.javasoft.com/beans.

VJS supports Java Bean components developed with JDK 1.1, but current versions of the Navigator and Composer do not support JDK 1.1. Java Bean components that use JDK 1.1 system level events will not run properly within these products.

Java Beans components usually provide a BeanInfo to permit inspection. If null is returned for the BeanInfo getEventSetDescriptors(), getPropertyDescriptors(), or getMethodDescriptors() methods, VJS uses automatic introspection to determine this metadata. Reflection is also employed to find setter and getter methods in accordance with the Java Beans naming conventions if a property descriptor does not provide this information.

### Adding Beans to Visual JavaScript

To use a Java Beans component to build VJS applications, follow these steps:

1. Package the Java Bean component and its supporting files into a JAR file. To create a JAR file, see Packaging a Component. VJS supports both compressed and uncompressed JAR files.

2. Import the Java Bean component to a Visual JavaScript palette. Choose Tools | Install to Palette | New, and select the JAR file containing the Java Bean component. Importing the Java Bean to the palette creates an <APPLET> tag with a FILEREFS attribute referring to all the files in the JAR. This ensures that supporting files get deployed with the component.

### Working with Bean Customizers

If a Java Bean defines a customizer interface, then when you import the Bean into VJS as a component, VJS instantiates a VJS copy of the customizer. This enables you to edit component properties in the VJS inspector using the custom interface defined by the customizer. When you open the inspector, it brings up the custom interface.

Edits you make in the VJS inspector are automatically passed back to the actual Java Bean customizer class, and VJS itself is updated to reflect the changes.

## Using the Built-In Database Components

VJS provides a set of simple Java Bean components that deliver database functionality. Two standard interfaces are used by these components, and any third party components that interact with them: the Row interface, and the Table interface. These interfaces use standard Java Bean conventions, and are designed to work in conjunction with the Java Bean PropertyChange mechanisms. These interfaces define a protocol that is bidirectional and that allows Java and JavaScript components which implement them to be easily connected on an HTML page using VJS.

The Row interface is used to define and transfer a set of name/value pairs, such as one row of a database table. This interface could be used, for example to connect a cursor object with a form object that displays one row of data at a time. The interface has two parts: one to specify the number of columns, their names and types; and another to manage the changing and transferring of data.

The Table interface is used by two components to exchange an entire collection of data. For example, a cursor and a spreadsheet component could use the Table interface to exchange data.

These interfaces, and various components that implement them (JavaDummyCursor, JavaForm, DummyCursor, TableApplet and ClientCursor) are provided as examples in the CDK's netscape.peas directory.

> NOTE: There are not different interfaces for data providers and data consumers.The same interface is used to send and receive data. This reduces the number of interfaces, adapters and support classes in the system.

#### Data Contexts

Even though the same interface is used for receiving and sending data, the Row and Table interfaces behave differently depending on their roles. re are different roles which components using the Row and Table interfaces assume. In addition to the sender and receiver roles, each of these interfaces acts in a data context, either as a consumer of data, or a provider of data. Data context refers to the number and names of available columns, the number of rows, the current row, and the numbering of those rows. If two components are connected using the Row or Table event, one must provide the data context, and the other consumes it.

For example, assume that a cursor is connected to a spreadsheet component, and that the spreadsheet component is also connected to a form component. The cursor is the data context provider, and the spreadsheet is the data context consumer. By connecting to the cursor, the spreadsheet inherits its data context, which it in turn provides to the form. Note that the spreadsheet is both a consumer and provider, and that a component's role in terms of data context has nothing to do with the direction of the data transfer.

#### Support Classes

Two Java classes, RowSupport and TableSupport are provided as support for the Row and Table interface components. These classes make it easy to implement the Row and Table interfaces in a Java Bean component. The classes are similar in nature to Java Bean adapters, but have slightly different uses.

These classes are generic support classes that are used by the JavaDummyCursor and JavaForm examples. Like the PropertyChangeSupport class in the Bean Developer's Kit (BDK), they can be used as members to which Row and Table methods may be delegated. It is also possible to extend these classes, provided, of course, that the Java Bean does not already extend some other class.

An additional use of these classes is to server as helper objects for JavaScript components. In order to connect to Java Objects, the DummyCursor JavaScript object employs an instance of RowSupport (created by using the JavaScript new operator) to hold its current row's data, and to manage the event interactions with Java objects. Without such an object, such a connection would not be possible.

[Contents] [Previous] [Next] [Index]

Last Updated: 09/03/97 11:33:49

Copyright © 1997 Netscape Communications Corporation