July 31, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

User Code: Java Applet and JavaScript Communications

  • June 22, 2001
  • By Zhou Wu
  • Send Email »
  • More Articles »

This article introduces a means for communicating between a Java applet and an HTML page. (Of course, we'll not use LiveConnect, which can only be used for Netscape.)

The communications is reached by overwriting the toString() and showStatus() methods from the Applet class. These methods can be called by JavaScript's applet objects. This may be not very elegant, but it works.

The Java applet code is here.


import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class TApplet extends Applet
{
      private TextArea paraArea = new TextArea(8,50);      
      private TextField info1 = new  TextField(20);
      private TextField info2 = new TextField(20);
      private String communication;
      private String getInfo;
      private String sendInfo;
    
    
      public void init()
       {
         Panel p2 = new Panel();
         Panel p3 = new Panel();
         p3.add(new Label("Name:"));
         p3.add(info1);
         p3.add(new Label("Phone:"));
         p3.add(info2);
         p2.add("Center", p3);
         add("South",  p2);  
       }

       // Method called by Java Script applet object to send infomation to the applet.
       public void showStatus(String s)
       {
               StringTokenizer st = new StringTokenizer(s, "|");
               info1.setText(st.nextToken());
               info2.setText(st.nextToken());
	             //super.showStatus(s);	
       }

        // Method called by Java Script applet object to get infomation to the applet.
       public String toString()
       {
          //t.setText(com);
          getInfo ="Name:" +  info1.getText() + " |";
          getInfo += "Phone:" +  info2.getText() + " |";
          return getInfo;
        }
}

Java Applet and HTML Page Communication

Demo

Downloads

Download source = 1.5 K

About the Author

Zhou Wu works for AT&T Wireless Service, Inc. More information.






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel