简体   繁体   中英

How to call Applet method from javascript

I have created an Applet and I am going to access applet method from my html page on web project.

Here my applet looks like:

public class MessageApplet extends Applet {
private Label m_mess;    
    public void init() 
    {
        setBackground(Color.lightGray);
        setLayout(new BorderLayout());
        m_mess = new Label("MessageApplet is Running... : No Selection Yet", Label.CENTER);
        add(BorderLayout.CENTER, m_mess);
    }    
    public void setMessage(String message)
    {
        m_mess.setText("Selection : " + message);
    } 
} 

And my html page looks like:

<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<SCRIPT LANGUAGE="JavaScript">
function selectedCity() 
{
    if(document.CityChoice.City[0].checked == true)
      {
        document.SimpleMessageApplet.setMessage(document.CityChoice.City[0].value);
    }      
}
</SCRIPT></HEAD>
<BODY >
<b>This is the Applet</b>
<APPLET CODE="MessageApplet.class" NAME="SimpleMessageApplet" WIDTH=350 HEIGHT=100 >
</APPLET >
<FORM NAME="CityChoice">
<input type="radio" name="City" value="Boston" onClick="selectedCity()"> Boston<br>
</form>
</BODY > 
</html>

but when I click radio button my browser get hang and I cannot access applet method ever. My applet class is in default directory and html is in WebContent folder. Please tell me what should I have change in my code?

The problem is the IF statement check:

document.CityChoice.City[0].checked == true

This is not exactly how it goes with Javascript since the wrong expression you have there throws an Error and it never makes it into the IF statement body.

I removed the IF statement and changed the code to something like this:

function selectedCity() 
{
    document.SimpleMessageApplet.setMessage("Hello");                
}

When I click I see the Hello message fine.

Change your HTML file content to something like:

<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Insert title here</title>
        <SCRIPT LANGUAGE="JavaScript">
            function selectedCity() 
            {
                var elem = document.getElementById('cityRb');

                if(elem.checked)
                {
                    document.SimpleMessageApplet.setMessage(elem.value);
                }      
            }
        </SCRIPT></HEAD>
    <BODY >
        <b>This is the Applet</b>
    <APPLET CODE="MessageApplet.class" NAME="SimpleMessageApplet" WIDTH=350 HEIGHT=100 >
    </APPLET >
    <FORM NAME="CityChoice">
        <input type="radio" id="cityRb" name="City" value="Boston" onClick="selectedCity()"> Boston<br>
    </form>
</BODY > 
</html>

Also adding the full class code:

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Label;

/**
 *
 * @author hmmmmm
 */
public class MessageApplet extends Applet {

    private Label m_mess;

    public void init() {
        setBackground(Color.lightGray);
        setLayout(new BorderLayout());
        m_mess = new Label("MessageApplet is Running... : No Selection Yet", Label.CENTER);
        add(BorderLayout.CENTER, m_mess);
        m_mess.setBackground(Color.red);
    }

    public void setMessage(String message) {
        m_mess.setText("Selection : " + message);
    }
}

The problem you get is on different browsers is that they have different implementations of the outdated LiveConnect (javascript<->java) technology.

As a rule of thumb, Firefox will be more cooperative when trying to do such things.

What your problem is is that you are trying to include your applet to the page in a very ancient way. Although it may and will work on some browsers, it is not the recommended way to include an applet to a page.

Java Web start is the tech stack and JNLP is the protocol you can use to distribute java content in a standardized way as you can read in this article:

http://en.wikipedia.org/wiki/Java_Web_Start

A deployment jnlp descriptor is the proper way you can use to embed your applet to a page. Also, it is a good idea to use Sun's deployJava.js script which will save you a lot of trouble when deploying your applet to a container on the page. (it's a bit restricted though so feel free to add stuff to it)

http://docs.oracle.com/javase/6/docs/technotes/guides/jweb/deployment_advice.html

All in all, a jnlp/Java web start powered applet is the way to go.Below is an example of a deployment descriptor.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <jnlp codebase="file:/C:/JavaApplication6/dist/" href="launch.jnlp" spec="1.0+">
        <information>
            <title>JavaApplication6</title>
            <description>blalbla</description>
            <description kind="short">JavaApplication6</description>

        </information>
    <update check="background"/>
    <security>
    <all-permissions/>
    </security>
        <resources>
    <j2se java-vm-args="-Djava.security.policy=applet.policy" version="1.5+"/>
    <jar href="JavaApplication6.jar" main="true"/>


        <jar href="lib/jna.jar"/>
    <jar href="lib/platform.jar"/>
    </resources>
        <applet-desc height="300" main-class="winToJnaApi.NewApplet" name="JavaApplication6" width="300">

        </applet-desc>
    </jnlp>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM