Accessing Java Content


document.JavaApplet.setString(field.value); 

Java was once thought to be the dominant web format, but nowadays it works more in the background than in the front-end. However, you still see some Java applets embedded in web pages from time to time, although the Java plug-in lacks the large penetration of the Flash plug-in.

When a Java applet is embedded in a page, you can call all public methods of this applet. The following, very simple applet exposes its setString() method, which sets a private variable and then repaints the applet:

A Simple Java Applet (JavaApplet.java)

import java.applet.*; import java.awt.*; public class JavaApplet extends Applet {   private String _s = "Welcome!";   public void paint(Graphics g) {     g.setColor(Color.black);     g.drawString(_s, 20, 20);   }   public void setString(String s) {     this._s = s;     this.repaint();   } } 

After this applet is compiled into a class, the following JavaScript code calls the setString() method whenever the content of the text field changes. Figure 12.3 shows the result.

Accessing Java with JavaScript (java.html)

<script language="JavaScript"   type="text/javascript"> function paint(field) {   document.JavaApplet.setString(field.value); } </script> <applet code="JavaApplet.class" name="JavaApplet"   width="150" height="75"></applet><br /> <input type="text" onkeyup="paint(this);" /> 

Figure 12.3. The applet changes while you are typing.





JavaScript Phrasebook(c) Essential Code and Commands
JavaScript Phrasebook
ISBN: 0672328801
EAN: 2147483647
Year: 2006
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net