We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpIntegration › Call Java function from Javascript
Page Index Toggle Pages: 1
Call Java function from Javascript (Read 1817 times)
Call Java function from Javascript
Aug 8th, 2007, 3:48am
 
Hello to you all,

this should be rather simple but I don't get it.
I want to my Javascript to call a Java function like

<script>
function snd()
{
 document.sketch_070808b_container.doSmthng("test");
}
</script>


This is the sketch code:

import netscape.javascript.*;

public MyClass test;

void setup()
{
   test = new MyClass( this );
   test.doSmthng("hello world");
}

public class MyClass {
   PApplet papplet;
   MyClass ( PApplet _p )
   {
  papplet = _p;
   }

   void doSmthng (String str) {
  papplet.println(str);
   }
}


It prints "hello world" on setup(), but it never reaches doSmthng() when called by the Javascript.

What am I doing wrong?

Cheers,
Ben
Re: Call Java function from Javascript
Reply #1 - Aug 8th, 2007, 8:59am
 
the method you are calling is inside a class, it needs to be on a global scope:

Code:

void setup() { .... }
void draw() { .... }

void doSmthng () { .... }

class MyClass { .... }


you can try to call document.sketch_070808b_container.test.doSmthng() too. be aware that javascript to java communication is rather unreliable .. and make sure your exported html has "mayscript" enabled (should already be set when you export with processing).

F
Re: Call Java function from Javascript
Reply #2 - Aug 8th, 2007, 12:36pm
 
Found it. Scriptable is set by default, but the code Processing generates doesn't create a DOM object for the applet. You can reproduce it if you generate any sketch and check with Firefox and Firebug that the element with name *_container is null. I tried to set an param id inside the applet's object, but no go.

So in short, this works (other than below the element here is known as "applet_one" not as *_container, but that doesn't change anything):

<applet id="applet_one" code="sketch_070808b" archive="sketch_070808b.jar" width="400" height="100" MAYSCRIPT="TRUE"></applet>

And this lengthy code produced by Processing doesn't work (sketch_070808b_container unknown, setting an param id inside the object won't help either):

Greets from Germany,
Ben

<div id="sketch_070808b_container">

<!--[if !IE]> -->
<object classid="java:sketch_070808b.class"
           type="application/x-java-applet"
           archive="sketch_070808b.jar"
           width="100" height="100"
           standby="Loading Processing software..." >
           
<param name="archive" value="sketch_070808b.jar" />

<param name="mayscript" value="true" />
<param name="scriptable" value="true" />

<param name="image" value="loading.gif" />
<param name="boxmessage" value="Loading Processing software..." />
<param name="boxbgcolor" value="#FFFFFF" />

<param name="test_string" value="outer" />
<!--<![endif]-->

<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
codebase="http://java.sun.com/update/1.4.2/jinstall-1_4_2_12-windows-i586.cab"
width="100" height="100"
standby="Loading Processing software..."  >

<param name="code" value="sketch_070808b" />
<param name="archive" value="sketch_070808b.jar" />

<param name="mayscript" value="true" />
<param name="scriptable" value="true" />

<param name="image" value="loading.gif" />
<param name="boxmessage" value="Loading Processing software..." />
<param name="boxbgcolor" value="#FFFFFF" />
Re: Call Java function from Javascript
Reply #3 - Aug 8th, 2007, 5:00pm
 
hmm .. i'm pretty sure the code produced by processing is ok. i think you have to go for document.applets[0] (applets array). did you check that?

i'm looking into the DOM problem as soon as i can. here's a bug for that:
http://dev.processing.org/bugs/show_bug.cgi?id=608

F
Re: Call Java function from Javascript
Reply #4 - Aug 19th, 2007, 9:41am
 
here's a test for FF to show it's working:

Code:

<!-- add this right after <body> -->
<script type="application/x-javascript" >
// add id="applet_outer"
// to first <object>
var pinit = function ( event )
{
var applet = document.getElementById( "applet_outer" );
console.log( applet.parentNode );
};
addEventListener( 'load', pinit, false );
</script>


the problem with your code is that you tried to call the functions on the container, which is a DIV not the actual applet (<OBJECT>).

here's some code that will list all nodes in the container, including the applet (OBJECT) and it's functions that are available to javascript:

Code:


// console.log() is firebug-addon for firefox
// you will need that ...

var container = document.getElementById( "sketch_070819a_container" );

for ( var num in container.childNodes )
{
console.log( " ---------------------------------- " );
console.log( container.childNodes[num] );
var node = container.childNodes[num].nodeName;

for ( var attr in container.childNodes[num] )
{
console.log( node + " " + attr + " " + container.childNodes[num][attr] );
};
};


F
Re: Call Java function from Javascript
Reply #5 - Aug 19th, 2007, 1:54pm
 
Yes, that was the problem. I found it myself later, I'm sorry that I didn't share it. It works with all browser I've tested (IE6, IE7, FF2, FF1.5, Konqueror) but Opera. But that's most propably because of it's limited Javascript engine, I tested with applet and object tag.

Maybe Processing could generate an id for the object by itself.

~Ben
Re: Call Java function from Javascript
Reply #6 - Aug 19th, 2007, 2:09pm
 
yeah i have the ID on my list .. have to update the templates for the next release.

can you post a link to what you've done?

thanks
F
Re: Call Java function from Javascript
Reply #7 - Aug 19th, 2007, 2:27pm
 
Sure,

http://estudy-portal.de/~wa-soziomat/applet/java.htm
http://estudy-portal.de/~wa-soziomat/test/java.html

The first uses the applet tag, the latter the object tag. Opera can't deal with the applet.
This is a GPL project. I can put the sources online if you like.

~Ben

fjen wrote on Aug 19th, 2007, 2:09pm:
yeah i have the ID on my list .. have to update the templates for the next release.

can you post a link to what you've done

thanks
F


Re: Call Java function from Javascript
Reply #8 - Aug 19th, 2007, 2:40pm
 
super. danke.

i don't need to see the sources, was just interessted in your javascript code. i think you have to define the onclick handler as:

onclick="snd1(); return false;"

a return value is assumed by the specs ... returning false will keep it from "bubbling up".

btw, on safari 2.x the <applet> version is not working

best
F
Page Index Toggle Pages: 1