3 Replies Latest reply: Nov 26, 2013 8:35 AM by jerry kramskoy RSS

    Can I pass html5 javascript action event to javafx?

    WangZi

      Can I pass html5 javascript action event to javafx?

       

      Instead of just displaying web page in javafx, can javafx interactive with html5 and javascript?

        • 1. Re: Can I pass html5 javascript action event to javafx?
          jerry kramskoy

          Yes, this is possible.


          Once a webpage has loaded, you need to add your java interface to the web engine ...


          private void addUpCallInterface(WebEngine webEngine)  {         

               JSObject win = (JSObject) webEngine.executeScript("window");          

                win.setMember("JFXInterface", new YourInterface());          

                /* as a result of this setMember, javascript can now contain statements like           

                 * JFXInterface.uiClick()           

                 */                

          }       


          public class YourInterface {             

           

               /**             

                * javascript calls back here             

                */         

               public void uiClick(String id) { 

                     /* ... user clicked on some HTML5 ui element with id = "id"   */

                    /* e.g. for the HTML below, id = "top" */    

                    uiId = id;                           

                }         


          }   


          In HTML ...   


           

          <div id="top" onclick="JFXInterface.uiClick();"> Top </div>

           

           

          Be careful that the string you provide to win,setMember above is identical with the one you refer to inside javascript.

           

            cheers, Jerry

          • 2. Re: Can I pass html5 javascript action event to javafx?
            WangZi

            Hi Jerry

             

            That's cool? How about javaFX pass event to html5 ?

             

             

             

            Regards,

            Hua Jie

            • 3. Re: Can I pass html5 javascript action event to javafx?
              jerry kramskoy

              Hi Hua Lie

               

              I haven't looked into passing events to HTML5, but you can certainly call javascript from JFX.

               

              Once the page is loaded, you can then do something like

               

              private void writeResult(String msg) {

                   if (msg != null) {

                        String script = "result(" + "'" + msg + "')";

                        webEngine.executeScript(script);

                   }

              }

               

              The above code must run on the JFX Application Thread,

               

              Here, the web page has a  javascript function result(s), which could edit the HTML5 to display a message in a div.

               

              I've used this technique to set the timeline of an HTML5 video element, controlling the video from JFX.

               

              It's also possible to integrate Swing with Web by using JFX.

               

              cheers, Jerry