Table of Contents | Previous | Next | Index


Packages

A top-level object used to access Java classes from within JavaScript code.

Core object

Implemented in

JavaScript 1.1, NES 2.0

Created by

The Packages object is a top-level, predefined JavaScript object. You can automatically access it without using a constructor or calling a method.

Description

The Packages object lets you access the public methods and fields of an arbitrary Java class from within JavaScript. The java, netscape, and sun properties represent the packages java.*, netscape.*, and sun.* respectively. Use standard Java dot notation to access the classes, methods, and fields in these packages. For example, you can access a constructor of the Frame class as follows:

var theFrame = new Packages.java.awt.Frame();
For convenience, JavaScript provides the top-level netscape, sun, and java objects that are synonyms for the Packages properties with the same names. Consequently, you can access Java classes in these packages without the Packages keyword, as follows:

var theFrame = new java.awt.Frame();
The className property represents the fully qualified path name of any other Java class that is available to JavaScript. You must use the Packages object to access classes outside the netscape, sun, and java packages.

Property Summary

Property Description
className

The fully qualified name of a Java class in a package other than netscape, java, or sun that is available to JavaScript.

java

Any class in the Java package java.*.

netscape

Any class in the Java package netscape.*.

sun

Any class in the Java package sun.*.

Examples

The following JavaScript function creates a Java dialog box:
function createWindow() {
   var theOwner = new Packages.java.awt.Frame();
   var theWindow = new Packages.java.awt.Dialog(theOwner);
   theWindow.setSize(350,200);
   theWindow.setTitle("Hello, World");
   theWindow.setVisible(true);
}
In the previous example, the function instantiates theWindow as a new Packages object. The setSize, setTitle, and setVisible methods are all available to JavaScript as public methods of java.awt.Dialog.


className

The fully qualified name of a Java class in a package other than netscape, java, or sun that is available to JavaScript.

Property of

Packages

Implemented in

JavaScript 1.1, NES 2.0

Syntax

Packages.className
where classname is the fully qualified name of a Java class.

Description

You must use the className property of the Packages object to access classes outside the netscape, sun, and java packages.

Examples

The following code accesses the constructor of the CorbaObject class in the myCompany package from JavaScript:

var theObject = new Packages.myCompany.CorbaObject()
In the previous example, the value of the className property is myCompany.CorbaObject, the fully qualified path name of the CorbaObject class.


java

Any class in the Java package java.*.

Property of

Packages

Implemented in

JavaScript 1.1, NES 2.0

Syntax

Packages.java

Description

Use the java property to access any class in the java package from within JavaScript. Note that the top-level object java is a synonym for Packages.java.

Examples

The following code accesses the constructor of the java.awt.Frame class:
var theOwner = new Packages.java.awt.Frame();
You can simplify this code by using the top-level java object to access the constructor as follows:

var theOwner = new java.awt.Frame();

netscape

Any class in the Java package netscape.*.

Property of

Packages

Implemented in

JavaScript 1.1, NES 2.0

Syntax

Packages.netscape

Description

Use the netscape property to access any class in the netscape package from within JavaScript. Note that the top-level object netscape is a synonym for Packages.netscape.

Examples

See the example for .Packages.java

sun

Any class in the Java package sun.*.

Property of

Packages

Implemented in

JavaScript 1.1, NES 2.0

Syntax

Packages.sun

Description

Use the sun property to access any class in the sun package from within JavaScript. Note that the top-level object sun is a synonym for Packages.sun.

Examples

See the example for .Packages.java

Table of Contents | Previous | Next | Index

Last Updated: 10/29/98 20:17:26

Copyright © 1998 Netscape Communications Corporation