Javascript for Java applet

I am trying to pass the selected value from the HTML drop-down list to the applet method using the setter method in the applet. But every time Javascript is called, it shows that "the object does not support this property or method" as an exception.

My javascript code is:

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 is:

 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 is:

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

Change ..

 document.decisiontreeapplet 

.. to ..

 document.getElementById('decisiontreeapplet') 

.. and this is likely to work.

eg.

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. Please note that the number of lines in the two sources shown above is shorter than yours, for example, the applet, and it took me longer to prepare the source so that I could check my answer.

+7
source

Try changing the id parameter in the applet tag instead.

 <applet name="decisiontreeapplet" ...> </applet> 
+1
source

I think the <applet> tag is deprecated and the <object> tag is used instead. I remember that in the object tag there was some logical parameter called scriptable .

Why aren't you using the deployment toolkit ? This will save you a lot of effort - see http://rostislav-matl.blogspot.com/2011/10/java-applets-building-with-maven.html for more information.

-1
source

All Articles