ACCESS TO JAVA-APPLET FROM JAVASCRIPT
By Anton, on September 13, 2008, in HOWTO
Do you know that you can call Java-Applet-public-methods from JavaScript?
For example, you have an applet:
import javax.swing.JApplet;
import javax.swing.JLabel;
public class TestApplet extends JApplet {
JLabel lbl;
public void setLabelText(String text) {
lbl.setText(text);
}
public void init() {
lbl = new JLabel("This is java-applet");
this.getContentPane().add(lbl);
}
}
If you want to change text in applet’s JLabel with JavaScript, you can use following code:
<APPLET code="TestApplet.class" width=240 height=40 id="myapplet"></APPLET>
<FORM>
<INPUT TYPE="text" value="Enter text here" id="myTextField">
<INPUT type=button value="Insert to applet" onclick="CallAppletMethod()">
<SCRIPT>
function CallAppletMethod() {
var myApplet = document.getElementById('myapplet');
var myParam = document.getElementById('myTextField').value;
myApplet.setLabelText(myParam); //just call java-method, it's easy :)
}
</SCRIPT>
I don’t really know how you can use it at present, but it works. Earlier I used it for some Ajax tricks. It was a time when was no another way to sent and receive data from the web-server without page-reloading.