HOWTO: Fire 'Events' from a Java Applet on a Web PageLast reviewed: January 29, 1998Article ID: Q178994 |
The information in this article applies to:
SUMMARYIn 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.0 documentation for more information.
MORE INFORMATIONIn 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:
<HTML> <HEAD> <SCRIPT LANGUAGE="VBScript"> <!-- Sub window_onLoad() ' Set the initial timeout for polling window.setTimeout "checkClicked", 1000 end sub ' This is the method that the timeout calls Sub checkClicked() if document.app.clicked then ' The applets clicked method returned True msgbox "You clicked my applet!" ' Reset the timeout window.setTimeout "checkClicked", 1000 else ' The applets clicked method returned False ' Reset the timeout window.setTimeout "checkClicked", 1000 end if end sub --> </SCRIPT> <TITLE>Document Title</TITLE> </HEAD> <BODY> <APPLET CODE=MyApplet.class ID=app WIDTH=320 HEIGHT=200> </APPLET> </BODY> </HTML>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 for Java 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.
REFERENCESFor more information about the Microsoft Virtual Machine for Java's AutoIDispatch feature, please see the following article in the Microsoft Knowledge Base: ARTICLE-ID: Q172202 TITLE : INFO: Implementing Java Automation Objects Using AutoIDispatchFor the latest Knowledge Base articles and other support information on Visual J++ and the SDK for Java, see the following page on the Microsoft Technical Support site:
http://support.microsoft.com/support/visualj/ http://support.microsoft.com/support/java/ |
Additional query words: fire events applet script
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |