Java Script - Java Applet Communication - Live Connect

Here is an example of how to communicate with Java Applet using Java Script (vice-versa)

//--------------------------------TestApplet.java------------------------------------------------

import netscape.javascript.*;
import java.applet.*;
import java.awt.*;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;

public class TestApplet extends JApplet implements ActionListener
{
    public String displayString = "India";

    public TestApplet() throws HeadlessException
    {
        super();
    }

    public void init()
    {
      
    }

    public void paint(Graphics g)
    {
        g.drawString("Hello world! " + displayString, 50, 25);
    }

    public void actionPerformed(ActionEvent arg0)
    {

    }

    public void ChangeText(String input)
    {
        try
        {
            displayString = input;
            repaint();
            System.out.println("screen refresh done");

            //call the java script function with the parameter list...
            JSObject win = JSObject.getWindow(this);
            //Parameter List...
            String args2[] = { "" };
            //Function Call
            win.call("testForm", args2);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}

//--------------------------------Test.html------------------------------------------------

<html>
 <head>
  <title>HTML Page</title>
  <SCRIPT>
  function testForm()
  {  
  alert("Call from Java Applet - Now the form is being submited");  
  document.forms[0].submit();
  }
  </SCRIPT>
 </head>
 <body bgcolor="#FFFFFF">
 <form method=post action=go.asp>
 <APPLET CODE="TestApplet.class" NAME="muthuapplet" WIDTH=150 HEIGHT=175></APPLET> 
    <hr>
    <A HREF="#" onClick="javascript:document.applets.muthuapplet.ChangeText('Europe');">Change</A><BR>
 </form>
 </body>
</html>



Save This Page Save This Page Digg!

Comments

Alena () (URL) - 07 October '06 - 14:50 - Reply on Alena

I will continue to visit enjoyed the reading thanks

Personal information





Remember your information?
Comment

To prevent automated commentspam we require you to answer this silly question
 


Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.