Friday, September 2, 2011

Java Applet/Javascript Communication

So I have established that for the last decade, not many people have used Applets in web pages. The resulting paucity tutorials or recent examples made it very difficult even to scratch the surface of how to do this. I know when I write notes here, I often skip steps, which seem too obvious or easy, so that when I revisit an entry written two years ago, even I find it hard to follow. So I can't criticize others for being cryptic, or telling only half the story. But it has been frustrating wading through articles with gaps, or code that doesn't work.

But where to begin with an attempt at a full story? I think a revisit to paths and class paths.

In order to compile and run java classes, you need to set a path to the JDK binaries folder. This can be done once at the start of a command window session, or even as a Windows environment variable.

In order to run classes which connect to a database, you need set a "class" path to a special .jar package containing classes pertaining to communication with the database, every time you run the class.

Java Applet/Javascript Communication requires yet another variant. In order to compile a class which will communicate with Javascript, you have to set a class path, every time you compile the class, to a .jar package with the all-informative name of plugin.jar. This contains the classes needed for an applet to call javascript functions. It can be found in /lib directory of the JRE installation (e.g. C:\Program Files\Java\jre7\lib). The command to compile the class myClass then becomes:

javac -classpath "C:\Program Files\Java\jre7\lib\plugin.jar" myClass.java

I wasted a lot of time adding plugin.jar to the JDK binaries folder, and to my code folder, and couldn't think why sample code cut and pasted from web pages wouldn't compile. It doesn't matter where the .jar file is, as long as a path to it is included with every compile command.

The second step included in all the articles on java applet to javascript communication is to add the import:

import netscape.javascript.*;

If you don't include a class path to plugin.jar in your compile command, this line will generate the first of many compile errors.

The third step is to write some code in the java applet which actually works. I hunted and hunted for this. The best example I found was on this web page. I should like give them credit for showing me a solution, but I have modified the code to look more like my earlier Hello World example. The applet code is:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import netscape.javascript.*;

public class HelloWorldB extends Applet {
String text="Hello world!";
JSObject win, doc, form, textField;

public void init() {
win = JSObject.getWindow(this);
doc = (JSObject) win.getMember("document");
form = (JSObject) doc.getMember("textForm");
textField = (JSObject) form.getMember("textField");
setLayout(new BorderLayout());
Panel buttons = new Panel();
Button displayTextButton = new Button("Display Text");
displayTextButton.addActionListener(new ButtonEventHandler());
buttons.add(displayTextButton);
add("South",buttons);
}
public void paint(Graphics g) {
g.drawString(text,30,30);
}

class ButtonEventHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
String s = e.getActionCommand();
if("Display Text".equals(s)) {
text= (String) textField.getMember("value");
repaint();
}
}
}
}

The fourth step included in the articles on java applet to javascript communication is to add "MAYSCRIPT" to the Applet tag.

The web code is then:

<html>
<body>

<applet code="HelloWorldB.class"
WIDTH=200 HEIGHT=70 MAYSCRIPT>
</applet>
<form NAME="textForm">
<P>Enter text and click button:<br>
<INPUT TYPE="text" NAME="textField" SIZE="20"></P>
</FORM>

</body>
</html>

This comes up as shown below:

Then you add text as shown below:

And when you click the button it come up as:

This example takes information from a web page and displays it in an applet. I am actually hoping to do the reverse, but it is a step in the right direction.

No comments: