Results 1 to 13 of 13
  1. #1
    sunyi000 is offline Member
    Join Date
    Aug 2010
    Posts
    7
    Rep Power
    0

    Default 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 03:10 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default

    What package(s) do you use in java to communicate with javascript?

    Your posted code leaves out a lot.

  3. #3
    sunyi000 is offline Member
    Join Date
    Aug 2010
    Posts
    7
    Rep Power
    0

    Default

    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

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default

    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?

  5. #5
    sunyi000 is offline Member
    Join Date
    Aug 2010
    Posts
    7
    Rep Power
    0

    Default

    import javax.script.ScriptEngine;
    import javax.script.ScriptEngineManager;
    import javax.script.ScriptException;

  6. #6
    sunyi000 is offline Member
    Join Date
    Aug 2010
    Posts
    7
    Rep Power
    0

    Default

    sorry, corrections: i passed a JPanel (not JButton) to the javascript functio, created a JButton in javascript and added it to the JPanel

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default

    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.

  8. #8
    sunyi000 is offline Member
    Join Date
    Aug 2010
    Posts
    7
    Rep Power
    0

    Default

    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 JPANEL
    Last edited by sunyi000; 08-10-2010 at 03:27 AM.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default

    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?

  10. #10
    sunyi000 is offline Member
    Join Date
    Aug 2010
    Posts
    7
    Rep Power
    0

    Default

    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

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default

    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?

  12. #12
    sunyi000 is offline Member
    Join Date
    Aug 2010
    Posts
    7
    Rep Power
    0

    Default

    Thanks I got it work...

    need to use javascript function as Java interface, in this case use javascript function to implement MouseAdapter interface.

    Thanks..

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,767
    Blog Entries
    7
    Rep Power
    21

    Default

    Have you ever had a look at BeanShell? It is interpreted Java and some more. I like it.

    kind regards,

    Jos

Similar Threads

  1. Communicating between Dialogs and Frames
    By frejon26 in forum Threads and Synchronization
    Replies: 1
    Last Post: 05-23-2010, 06:49 AM
  2. Replies: 0
    Last Post: 04-27-2010, 01:15 PM
  3. Java Server & C# client communicating through sockets
    By yukijocelyn in forum Networking
    Replies: 0
    Last Post: 09-05-2008, 11:05 PM
  4. Replies: 2
    Last Post: 08-27-2008, 04:24 PM
  5. Communicating with JSP and popup
    By nilz in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-20-2007, 05:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •