Javascript到Java Applet的通信

时间:2011-09-02 02:14:59

标签: java javascript applet

我正在尝试使用Applet中的setter方法将选定的值从HTML下拉列表传递给Applet方法。但每次调用Javascript时,它都会显示“对象不支持此属性或方法”作为例外。

我的javascript代码:

function showSelected(value){
    alert("the value given from"+value);
    var diseasename=value;
    alert(diseasename);
    document.decisiontreeapplet.setDieasename(diseasename);

    alert("i am after value set ");
}

我的小程序代码:

package com.vaannila.utility;


import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;


import prefuse.util.ui.JPrefuseApplet;

public class dynamicTreeApplet extends JPrefuseApplet {

    private static final long serialVersionUID = 1L;
    public static int i = 1;
    public String dieasenameencode;
    //System.out.println("asjdjkhcd"+dieasenameencode);
    public void init() {
        System.out.println("asjdjkhcd"+dieasenameencode);
        System.out.println("the value of i is " + i);
        URL url = null;
        //String ashu=this.getParameter("dieasenmae");
        //System.out.println("the value of the dieases is "+ashu);

        //Here dieasesname is important to make the page refresh happen 

        //String dencode = dieasenameencode.trim();
        try {
            //String dieasename = URLEncoder.encode(dencode, "UTF-8");
            // i want this piece of the code to be called 
            url = new URL("http://localhost:8080/docRuleToolProtocol/appletRefreshAction.do?dieasename="+dieasenameencode);
            URLConnection con = url.openConnection();
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setUseCaches(false);
            InputStream ois = con.getInputStream();
            this.setContentPane(dynamicView.demo(ois, "name"));
            ois.close();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException f) {
            f.printStackTrace();

        } catch (IOException io) {
            io.printStackTrace();
        }
        ++i;
    }

    public void setDieasename(String message){
        System.out.println("atleast i am here and call is made ");
        this.dieasenameencode=message;
        System.out.println("the final value of the dieasenmae"+dieasenameencode);

    }

}

我的appletdeployment代码:

<applet id="decisiontreeapplet" code="com.vaannila.utility.dynamicTreeApplet.class" archive="./appletjars/dynamictree.jar, ./appletjars/prefuse.jar" width ="1000" height="500" >    
</applet>

4 个答案:

答案 0 :(得分:7)

改变..

document.decisiontreeapplet

.. to ..

document.getElementById('decisiontreeapplet')

..它很可能会奏效。

E.G。

HTML

<html>
<body>
<script type='text/javascript'>
function callApplet() {
    msg = document.getElementById('input').value;
    applet = document.getElementById('output');
    applet.setMessage(msg);
}
</script>
<input id='input' type='text' size=20 onchange='callApplet()'>
<br>
<applet
    id='output'
    code='CallApplet'
    width=120
    height=20>
</applet>
</body>
</html>

爪哇

import javax.swing.*;

public class CallApplet extends JApplet {

    JTextField output;

    public void init() {
        output = new JTextField(20);
        add(output);
        validate();
    }

    public void setMessage(String message) {
        output.setText(message);
    }
}

请考虑下次发布简短的完整示例。请注意,上面显示的两个来源中的行数比您的小程序,我花了更长的时间准备来源,所以我可以检查我的答案。

答案 1 :(得分:1)

尝试将applet标记中的 id 参数更改为 name

<applet name="decisiontreeapplet" ...>
</applet>

答案 2 :(得分:-1)

答案 3 :(得分:-1)

我认为&lt; applet&gt;标签已过时,而&lt; object&gt; 标签则需要使用。我记得在object标签中有一些名为 scriptable 的布尔参数。

为什么不使用部署工具包?它可以为您节省大量的时间 - 请参阅http://rostislav-matl.blogspot.com/2011/10/java-applets-building-with-maven.html以获取更多信息。