Results 1 to 13 of 13
- 08-10-2010, 01:42 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 7
- Rep Power
- 0
java communicating with javascript
Hi,
I have a swing application communicating with javascript through java scripting engine. It works fine. I pass a JPanel to the javascript function and created a JButton in javascript, what I want to do is to display a message when button is clicked. I know very little about javascript so my question is how could I add a button click event to the JButton in Javascript? something like b.addMouseListener.......
Thanks my code is pasted below
importPackage(javax.swing);
importPackage(java.awt);
importPackage(java.awt.event);
function addButton(jf)
{
var b=new JButton("Click me");
b.setButtonStyle(JideButton.TOOLBOX_STYLE);
b.setPreferredSize(new Dimension(110,30));
b.setMaximumSize(new Dimension(110,30));
//button click event here
jf.add(b);
}Last edited by sunyi000; 08-10-2010 at 02:10 AM.
- 08-10-2010, 01:52 AM #2
What package(s) do you use in java to communicate with javascript?
Your posted code leaves out a lot.
- 08-10-2010, 01:54 AM #3
Member
- Join Date
- Aug 2010
- Posts
- 7
- Rep Power
- 0
im using standard java scripting engine to communicate with javascript..
the communication works fine.. my question is how do I add a button click event to JButton in javascript?
the posted code is just a function in javascript
- 08-10-2010, 01:56 AM #4
What package(s) do you use in java to communicate with javascript? I was wondering how to research the classes.
What import statement do you use?
- 08-10-2010, 01:57 AM #5
Member
- Join Date
- Aug 2010
- Posts
- 7
- Rep Power
- 0
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
- 08-10-2010, 02:09 AM #6
Member
- Join Date
- Aug 2010
- Posts
- 7
- Rep Power
- 0
sorry, corrections: i passed a JPanel (not JButton) to the javascript functio, created a JButton in javascript and added it to the JPanel
- 08-10-2010, 02:15 AM #7
Could you post the String you pass to the eval() method that shows a button using javascript?
Your posted code sample leaves a lot out.
- 08-10-2010, 02:22 AM #8
Member
- Join Date
- Aug 2010
- Posts
- 7
- Rep Power
- 0
the string is the javascript code i posted above..it is js file in C:\ drive, i read this file into a string and then pass the string to the eval..should I post my question to the javascript forum???
ScriptEngineManager manager=new ScriptEngineManager();
ScriptEngine se=manager.getEngineByName("js");
String scr = "importPackage(javax.swing);importPackage(java.awt );"+
"importPackage(java.awt.event);" +
"function addButton(jf){" +
"var b=new JButton("Click me");"+
"b.setPreferredSize(new Dimension(110,30));"+
"b.setMaximumSize(new Dimension(110,30)); "+
"jf.add(b);"+
"}";
se.eval(scr);
Invocable invocableEngine = (Invocable)se;
invocableEngine.invokeFunction("addButton", p); //------p IS A JPANELLast edited by sunyi000; 08-10-2010 at 02:27 AM.
- 08-10-2010, 02:42 AM #9
Thanks, I can see a button on a panel now. But why?
If you don't know javascript, why are you trying to create a java program that can talk to a javascript program? How do you plan to use this?
- 08-10-2010, 02:57 AM #10
Member
- Join Date
- Aug 2010
- Posts
- 7
- Rep Power
- 0
i want to make my swing application configurable.. i.e. if the js file exist in a pre-defined directory, execute the script..if there's no script exist in that directory, do nothing.
I previously tried to used scala as the scripting language but it doesn't compliant with the jsr-223 frame work. The standard java scripting engine uses rhino which is javascript, so I switched to javascript. according to this link https://developer.mozilla.org/en/Scripting_Java
there must be some way to do what I want, I just couldn't get it work properly. Have a look at the section "JavaScript Functions as Java Interfaces". Thanks
- 08-10-2010, 03:04 AM #11
Would the binding between your Swing program and the javascript have to be very tight?
For example your current project requires a JPanel.
Why the script (that you don't know) vs pure java that you do?
- 08-10-2010, 03:33 AM #12
Member
- Join Date
- Aug 2010
- Posts
- 7
- Rep Power
- 0
Thanks I got it work...
need to use javascript function as Java interface, in this case use javascript function to implement MouseAdapter interface.
Thanks..
- 08-10-2010, 07:44 AM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Have you ever had a look at BeanShell? It is interpreted Java and some more. I like it.
kind regards,
Jos
Similar Threads
-
Communicating between Dialogs and Frames
By frejon26 in forum Threads and SynchronizationReplies: 1Last Post: 05-23-2010, 05:49 AM -
Communicating to database through URLconnection class
By sanoj_av in forum NetworkingReplies: 0Last Post: 04-27-2010, 12:15 PM -
Java Server & C# client communicating through sockets
By yukijocelyn in forum NetworkingReplies: 0Last Post: 09-05-2008, 10:05 PM -
Communicating Java Server and C client using char buffer
By tryit0704 in forum NetworkingReplies: 2Last Post: 08-27-2008, 03:24 PM -
Communicating with JSP and popup
By nilz in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 11-20-2007, 04:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks