Javascript来的Java Applet通信 [英] Javascript to Java Applet communication

查看:158
本文介绍了Javascript来的Java Applet通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从HTML下拉列表选择的值传递给Applet的方法,用setter方法​​中的applet。但每次的JavaScript调用它时显示对象不支持此属性或方法作为例外。

我的JavaScript code:

 函数showSelected(值){
    警报(+价值,从给定的值);
    VAR diseasename =价值;
    警报(diseasename);
    document.decisiontreeapplet.setDieasename(diseasename);    警报(我是值集后);
}

我的小程序code:

 包com.vaannila.utility;
进口java.io.FileNotFoundException;
进口java.io.IOException异常;
进口的java.io.InputStream;进口java.net.MalformedURLException;
进口的java.net.URL;
进口java.net.URLConnection中;
进口prefuse.util.ui.J prefuseApplet;公共类dynamicTreeApplet扩展Ĵ$ P $ {pfuseApplet    私有静态最后的serialVersionUID长1L =;
    公共静态INT I = 1;
    公共字符串dieasenameen code;
    //System.out.println(\"asjdjkhcd\"+dieasenameen$c$c);
    公共无效的init(){
        的System.out.println(asjdjkhcd+ dieasenameen code);
        的System.out.println(i的值是+ I);
        网址URL = NULL;
        //串阿树= this.getParameter(dieasenmae);
        在dieases的//System.out.println(\"the值为+阿树)        //这里dieasesname重要的是使页面刷新发生        //字符串书房code = dieasenameen code.trim();
        尝试{
            //字符串dieasename = URLEn coder.en code(DEN code,UTF-8);
            //我想这块code的被称为
            URL =新URL(\"http://localhost:8080/docRuleToolProtocol/appletRefreshAction.do?dieasename=\"+dieasenameen$c$c);
            URLConnection的CON = url.openConnection();
            con.setDoOutput(真);
            con.setDoInput(真);
            con.setUseCaches(假);
            InputStream的OIS = con.getInputStream();
            this.setContentPane(dynamicView.demo(OIS,名));
            ois.close();        }赶上(MalformedURLException的E){
            e.printStackTrace();
        }赶上(FileNotFoundException异常F){
            f.printStackTrace();        }赶上(IOException异常IO){
            io.printStackTrace();
        }
        ++我;
    }    公共无效setDieasename(字符串消息){
        的System.out.println(至少我在这里,并调用了);
        this.dieasenameen code =消息;
        的System.out.println(+ dieasenameen code的dieasenmae的最终值);    }}

我appletdeployment code:

 <小程序ID =decisiontreeappletcode =com.vaannila.utility.dynamicTreeApplet.class档案=./ appletjars / dynamictree.jar,./appletjars /$p$pfuse.jarWIDTH =1000HEIGHT =500>
< /小程序>


解决方案

更改...

  document.decisiontreeapplet

...到...

 的document.getElementById('decisiontreeapplet')

..它最有可能的工作。

例如

HTML

 < HTML和GT;
<身体GT;
<脚本类型=文/ JavaScript的'>
功能callApplet(){
    。味精=的document.getElementById('输入')值;
    小程序=的document.getElementById('输出');
    applet.setMessage(MSG);
}
< / SCRIPT>
<输入ID =输入类型=文本大小= 20的onchange ='callApplet()'>
< BR>
<小程序
    ID ='输出'
    code ='CallApplet
    宽度= 120
    高度= 20>
< /小程序>
< /身体GT;
< / HTML>

的Java

 进口的javax.swing *。公共类CallApplet扩展JApplet的{    JTextField的输出;    公共无效的init(){
        输出=新的JTextField(20);
        加(输出);
        验证();
    }    公共无效setMessage(字符串消息){
        output.setText(消息);
    }
}


也请考虑下一次发布一个简短完整的例子。注意,在上面所示的两个源线的数目,是较短您例如小程序,我花了更长的时间来prepare源,所以我可以检查我的答案。

I am trying to pass a selected value from HTML drop-down to an Applet method, using setter method in the Applet. But every time the Javascript is invoked it shows "object doesn't support this property or method" as an exception.

My javascript code :

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 :

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 :

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

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>

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 e.g. applet, and it took me longer to prepare the source so I could check my answer.

这篇关于Javascript来的Java Applet通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆