Javascript to Java Applet communication

11,740

Solution 1

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.

Solution 2

Try changing the id parameter in your applet tag to name instead.

<applet name="decisiontreeapplet" ...>
</applet>
Share:
11,740
bhalkian
Author by

bhalkian

I am java developer with novice knowledge in android

Updated on August 21, 2022

Comments

  • bhalkian
    bhalkian over 1 year

    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>
    
  • bhalkian
    bhalkian over 12 years
    hi andrew its not working even now everytime i try to invoke setmethod in my applet object its saying object doesnot support this method ..is their any way to debug this .
  • Andrew Thompson
    Andrew Thompson over 12 years
    "its not working" The example shown above works for me in FF. "is their any way to debug this(?)" If using FF, open the Error Console (Ctrl+Shift+J).
  • bhalkian
    bhalkian over 12 years
    was a cache problem .it worked for me as well :) thanks a ton