Simple Java applet control

Last updated: 08.05.2002


Netscape's LiveConnect feature allows JavaScript to control Java applets by means of their public methods. This example shows a simple applet, the Chameleon Bar being controlled by JavaScript. You can start and stop the applet, display information about it, and even change the speed at which it switches colors. Note that you need both Java and JavaScript enabled. Moreover, you need Navigator or Communicator from Netscape: Microsoft's JScript implementation on Internet Explorer does not appear to allow control of Java applets.


Execution [Stop] [Start] [Show Info]
Speed Control [Slowest] [Slow] [Normal] [Fast] [Fastest]

Control of the applet is achieved by accessing it through the document.applets[] array and calling its public methods as if they were JavaScript functions. For example, to set the delay between color updates to 100, the setDelay method of the applet would be called with the appropriate value. If the applet is named applet1, then the corresponding JavaScript expression would be:

     document.applets[applet1].setDelay(100);

JavaScript only has access to the public methods and variables of the applet class. It can't access any private methods or variables in any way, so if you want to control an applet with JavaScript, the relevant items need to be made public when the applet is built.


[raingod:resources:javascript] -- [up][links][home]