简体   繁体   English

Javascript到Java Applet的通信

[英]Javascript to Java Applet communication

I am trying to pass a selected value from HTML drop-down to an Applet method, using setter method in the Applet. 我试图使用Applet中的setter方法将选定的值从HTML下拉列表传递给Applet方法。 But every time the Javascript is invoked it shows "object doesn't support this property or method" as an exception. 但每次调用Javascript时,它都会将“对象不支持此属性或方法”显示为异常。

My javascript code : 我的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 ");
}

My applet code : 我的applet代码:

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);

    }

}

My appletdeployment code : 我的appletdeployment代码:

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

Change.. 更改..

document.decisiontreeapplet

..to.. ..至..

document.getElementById('decisiontreeapplet')

..and it will most likely work. ..它很可能会奏效。

EG 例如

HTML 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>

Java Java的

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);
    }
}

Please also consider posting a short complete example next time. 还请考虑下次发布简短的完整示例。 Note that the number of lines in the two sources shown above, is shorter that your eg applet, and it took me longer to prepare the source so I could check my answer. 请注意,上面显示的两个源中的行数比您的applet短,并且我花了更长的时间来准备源代码以便我可以检查我的答案。

Try changing the id parameter in your applet tag to name instead. 请尝试将applet标记中的id参数更改为name

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

I think the <applet> tag is obsolete and <object> tag shoudl be used instead. 我认为<applet>标签已过时,而<object>标签应该被使用。 I recall there was some boolean param named scriptable in the object tag. 我记得在object标签中有一些名为scriptable的布尔参数。

Why you do not use deployment toolkit ? 为什么不使用部署工具包 It would save you a lot of trying - see http://rostislav-matl.blogspot.com/2011/10/java-applets-building-with-maven.html for more info. 它可以为您节省大量的时间 - 请参阅http://rostislav-matl.blogspot.com/2011/10/java-applets-building-with-maven.html以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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