Microsoft KB Archive/178994

= How To Fire 'Events' from a Java Applet on a Web Page =

Article ID: 178994

Article Last Modified on 8/25/2005

-

APPLIES TO


 * Microsoft Java Virtual Machine
 * Microsoft Visual Basic, Scripting Edition 2.0
 * Microsoft JScript 1.0
 * Microsoft JScript 2.0
 * Microsoft Internet Explorer 3.01
 * Microsoft Internet Explorer 3.02

-



This article was previously published under Q178994



SUMMARY
In Internet Explorer 3.0x, there is no way for a Java Applet to "fire" an event that can be handled through scripting on a Web page. This article shows how to use combination of Java class methods and scripting methods to simulate events.

NOTE: In Internet Explorer 4.0, events can be fired from a Java applet and caught in the Web pages script. See the Microsoft SDK for Java 2.0x or later documentation for more information.



MORE INFORMATION
In order to simulate an event from a Java applet, you will need to create a set of methods in your Java Applet class and a polling function on the Web page. The polling function will check the applet to see if the event has occurred and act on that event if it has occurred.

For example, if you wanted to fire an event when the users clicks on the applet, you would code the class as follows: import java.awt.*; import java.applet.*; public class MyApplet extends Applet {  // fire is the private member that determines if the event fired private boolean fire = false;

public MyApplet {     // Set the background to black so it can be seen setBackground(Color.black); }

public boolean handleEvent(Event e)  { if(e.id == e.MOUSE_DOWN) {        // When the user clicks, set fire to True. fire = true; }     return false; }

// Scripting will call this method to see if the event occurred. public boolean clicked {     // If fire is True, then 'reset' it to False and return True if(fire) {        fire = false; return true; }     // Otherwise, return False return false; } } Then, you need to create HTML:   



Document Title 



 

  When the Web page is loaded, the window_onLoad function executea. It sets a "timeout" to execute the checkClicked VBScript function. The checkClicked function "polls" the Applet by calling the clicked method on the Applet. If the clicked method returns True, then the checkClicked displays a message box and resets the timeout. If the clicked method returned False, then the checkClicked function just resets the timeout.

Both VBScript and JScript can access public members and methods of the Java Applet due to a feature of the Microsoft virtual machine called AutoIDispatch. If you needed to simulate "Event Parameters," such as coordinates of the mouse click, you could add additional public methods on the Applet that returned these values.

