HomeActive Query BuilderJava to Javascript calling

Java to Javascript calling


Can anyone give me a hint or better yet a code fragment that will show me how to call into an existing JavaScript function on the page my ActiveQueryBuilder applet is running please?

This seems to be possible as the Java demo page does it (by calling a JavaScript function called updateTextPane()) but I am unable to find a working way of doing this!

Thanks in advance ...
Andrew Cockburn | email
October 18 2010, 12:58 PM
1. Add plugin.jar library into your applet project. You can find it in JRE or JDK installation directory.
2. Add line "import netscape.javascript.*;" into your code.
3. Call JS function in following way:

JSObject win = JSObject.getWindow(YourApplet.this);
win.call("jsFunctionName", new Object[] { functionParam1, functionParam2, ... });
Eugene Starkov | email
13 hours, 7 mins since original post
Thankyou very much - that's the 3rd way I didn't try yet because it required an additional jar, but no big deal. Thanks again!
Andrew Cockburn
18 hours, 49 mins since original post
I'm still unable to get this to work ...

I know that it is possible as it works fine on the demo page, so my execution environment can't be the issue. Every time I try to run the applet with this snippet of code in it:

public void sqlUpdatedEventOccurred(SQLUpdatedEvent event)
{
try
{
JSObject win = JSObject.getWindow(AnalystApplet.this);
win.eval("alert('hello init!'");

jTextPane1.setText(plainTextSQLBuilder1.getSQL());
}
catch (QueryBuilderException ex)
{
JOptionPane.showMessageDialog(AnalystApplet.this, ex.getMessage());
}
}

I get this exception:

java.lang.reflect.InvocationTargetException
at java.awt.EventQueue.invokeAndWait(Unknown Source)
at analystapplet.AnalystApplet.init(AnalystApplet.java:42)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: netscape.javascript.JSException
at netscape.javascript.JSObject.getWindow(Unknown Source)
at analystapplet.AnalystApplet$1$1.sqlUpdatedEventOccurred(AnalystApplet.java:71)
at com.adbs.querybuilder.BaseSQLBuilder.fireSQLUpdatedEvent(ml:232)
at com.adbs.querybuilder.BaseSQLBuilder.onSQLUpdated(ml:420)
at com.adbs.querybuilder.QueryBuilder.g(hl:2173)
at com.adbs.querybuilder.Query.doAfterUpdated(kl:86)
at com.adbs.querybuilder.ControlOwner.B(rj:270)
at com.adbs.querybuilder.ControlOwner.c(rj:307)
at com.adbs.querybuilder.ControlOwner.endUpdate(rj:1113)
at com.adbs.querybuilder.QueryBuilder.endUpdate(hl:1912)
at com.adbs.querybuilder.QueryBuilder.G(hl:2612)
at com.adbs.querybuilder.QueryBuilder.a(hl:523)
at com.adbs.querybuilder.QueryBuilder.setSQL(hl:499)
at analystapplet.AnalystApplet$1.run(AnalystApplet.java:108)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at java.applet.Applet.getParameter(Unknown Source)
... 22 more

I can't seem to get any more info out of the exception other than the stack trace itself. The only differences between my setup and the demo that I can see is that you have all your code in a single JAR file, and I have 3 (m applet, AciveQueryBuilder.jar and plugin.jar). Also, I don't know what environment you compiled the JAR file in - I am using linux and java 1.6.0_22.

After the exception is thrown, the rest of AQB seems to work fine, and I can call into it using JavaScript, just not go the other way, from the Applet into the Browser.

Any assistance here would be very welcome as I am failing at the very first hurdle!

Thanks,

Andrew
Andrew Cockburn | email
4 days, 3 hours since original post
Hello Andrew,

Did you tried in different browsers?
Also check the Java console for possible errors.
Eugene Starkov | email
6 days, 17 hours since original post
I don't think the browser is an issue although I have tried 3 different ones - as I said in the OP, the execution environment is fine as the demo works for me ok. For now I have worked around it by calling into the applet to grab the SQL.
Andrew Cockburn | email
6 days, 23 hours since original post
Have you added MAYSCRIPT attribute inside the APPLET tag? It's critical.
Eugene Starkov | email
1 week since original post
I did have it yes - I suspect there is some subtlety of the AWT event model that is eluding me - maybe there are restrictions as to where JSObject can be used in this model (I'm not an applet expert by any means)
Andrew Cockburn | email
1 week since original post
Here is the source code of our live demo applet:
http://activequerybuilder.com/files/demos/LiveDemoApplet.java
It works without problems.
Eugene Starkov | email
1 week, 1 day since original post
Thanks for all your help - it turns out that I was doing the right things all along, the problem is a severe conflict with the JQuery framework. Moving my applet out of a JQuery tab has improved everything beyond recognition and solved several intermittent and nagging problems, as well as this issue.

I hope to find a workaround for this, but for the moment, AQB is working very well and doing exactly what I need.
Andrew Cockburn | email
1 week, 6 days since original post
This topic is closed