Applet calling javascript methods.


Javascript Upload

Look and Feels and UIDefaults

Self signed applets

In the previous step we looked at how an applet's method may be invoked by a javascript. We have also looked at what sort of HTML tags should be used to enable java - javascript interaction. Now we will finish the show by looking at how a java applet may invoke a javascript function to update a web page.

In order to call javascripts we need to get hold of a JSObject. But first things first, we need to import the netscape.javascript.JSObject into our class. Don't let the name send shivers down your spine, this works with IE.

    public void init()
        jso = JSObject.getWindow(this);

Online Demo
Sponsored Content

Rad Upload - A drag and drop file upload applet.

All that remains to do is to add the JButton to our applet. Then we add an ActionListener to the JButton, and it's actionPerformed method calls the javascript.

	public void actionPerformed(ActionEvent e) {
                if(jso != null )
                    try {
              "updateWebPage", new String[] {txt.getText()});
                    catch (Exception ex) {

That ladies and gentlemen, is really all there is to it. The above code can also be written as a one liner. The try catch and the is null comparision are all extras. They are not strictly needed but it doesn't hurt to have them.

Now that we have come to the end of this article, you can download the code.

Introduction  |  Calling Applet Methods |  Calling javascripts
Copyright © Raditha Dissanayake 2013