ChrisHenrich
Programmer
I am developing a web page in which I want to enable the reader to interact with a Java applet thru checkboxes outside the applet. I understand that JavaScript functions can call Applet methods, but it isn't working for me.
Here is a simple example: the html, then the JavaScript file, then the Java source.
---------------------example1.html
<HTML>
<HEAD>
<TITLE>Simple Swing Applet Example</TITLE>
<script type="text/javascript" src="TalkToApplet.js" >
</script>
</HEAD>
<BODY>
<APPLET archive="TalkToApplet.jar" code="TalkToApplet.class"
id="myapplet" width="300" height="150"
mayscript="mayscript">
Your browser does not support Java, so nothing is displayed.
</APPLET>
<input type="checkbox" onclick="sendWord('stuff'); return true;" >
</input>Send word to applet
</BODY>
</HTML>
-----------------------TalkToApplet.js
function sendWord(aWord)
{
var theAp = document.getElementById("myapplet");
var theAp1 = document.applets[0];
theAp.receiveWord(aWord);
return true;
}
------------------------TalkToApplet.java
import javax.swing.*;
public class TalkToApplet extends JApplet {
JTextArea showWord;
public void init() {
showWord = new JTextArea(1,10);
getContentPane().add(showWord);
showWord.setText("empty");
repaint();
}
public void receiveWord(String aWord){
showWord.setText(aWord);
repaint();
}
}
---------------------------------------------------
Working on Apple with Mac OS X, I have tested this with Safari, Internet Explorer, and Netscape 7.2. It fails on all three (hey, that's portability). On Netscape, I can examine the code with the JavaScript Debugger, which shows me that my variables "theAp" and "theAp1" both point to the intended applet tag in the HTML - but the attempt to call receiveWord() fails with an error message like this:
``TypeError: theAp.receiveWord is not a function''
Presumably I am leaving out some required thing, but what?
Thanks in advance to anybody who can enlighten me.
Here is a simple example: the html, then the JavaScript file, then the Java source.
---------------------example1.html
<HTML>
<HEAD>
<TITLE>Simple Swing Applet Example</TITLE>
<script type="text/javascript" src="TalkToApplet.js" >
</script>
</HEAD>
<BODY>
<APPLET archive="TalkToApplet.jar" code="TalkToApplet.class"
id="myapplet" width="300" height="150"
mayscript="mayscript">
Your browser does not support Java, so nothing is displayed.
</APPLET>
<input type="checkbox" onclick="sendWord('stuff'); return true;" >
</input>Send word to applet
</BODY>
</HTML>
-----------------------TalkToApplet.js
function sendWord(aWord)
{
var theAp = document.getElementById("myapplet");
var theAp1 = document.applets[0];
theAp.receiveWord(aWord);
return true;
}
------------------------TalkToApplet.java
import javax.swing.*;
public class TalkToApplet extends JApplet {
JTextArea showWord;
public void init() {
showWord = new JTextArea(1,10);
getContentPane().add(showWord);
showWord.setText("empty");
repaint();
}
public void receiveWord(String aWord){
showWord.setText(aWord);
repaint();
}
}
---------------------------------------------------
Working on Apple with Mac OS X, I have tested this with Safari, Internet Explorer, and Netscape 7.2. It fails on all three (hey, that's portability). On Netscape, I can examine the code with the JavaScript Debugger, which shows me that my variables "theAp" and "theAp1" both point to the intended applet tag in the HTML - but the attempt to call receiveWord() fails with an error message like this:
``TypeError: theAp.receiveWord is not a function''
Presumably I am leaving out some required thing, but what?
Thanks in advance to anybody who can enlighten me.