Previous Contents Index Next |
Core JavaScript Reference 1.5 |
A top-level object used to access Java classes from within JavaScript code.
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
netscape
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.
The fully qualified name of a Java class in a package other than netscape, java, or sun that is available to JavaScript.
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.
Any class in the Java package 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();
Any class in the Java package 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
Any class in the Java package 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
Previous Contents Index Next
Copyright © 2000 Netscape Communications Corp. All rights reserved.
Last Updated September 28, 2000