Real'sHowTo AddThis Feed Button
Custom Search

Create dynamic HTML from a Java appletTag(s): Javascript interaction


We can use the netscape.javascript.* included with Netscape browser and IE4 (Win version). See also this HowTo to learn how to compile with this package.

[Java applet JDK1.1 Netscape/IE4 (win) OK]

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


public class HtmlFromJava extends Applet
  implements ActionListener {
  Button aButton;

  public void init(){
    setLayout(new FlowLayout());
    aButton = new Button("create HTML");
    add(aButton);
    aButton.addActionListener(this);
    }

  public void actionPerformed(ActionEvent ae) {
    if (ae.getSource() == aButton){
       String HTML =  "<HTML><HEAD></HEAD><BODY>";
       HTML += "<TABLE BORDER=1><TR><TD>Hello world</TD></TR></TABLE>";
       HTML += "</BODY></HTML>";
       JSObject win = (JSObject)JSObject.getWindow(this);
       win.eval("createHTML(\"" + HTML +"\");");
       }
    }
}

[Javascript and HTML]

<HTML><HEAD></HEAD><BODY>
<SCRIPT>
function createHTML(s) {
   win = window.open("about:");
   win.document.write(s);
   win.document.close();
   }
</SCRIPT>

<APPLET CODE=HtmlFromJava.class
        MAYSCRIPT
        WIDTH=150
        HEIGHT=150>
</APPLET></BODY></HTML>
Try it here
blog comments powered by Disqus


If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2014
[ home ]