This discussion is archived
7 Replies Latest reply: Mar 5, 2013 4:42 AM by James_D RSS

How to open browser or execute Javascript from Javafx app

992312 Newbie
Currently Being Moderated
Hi,


Im trying to open an url (that contains a sound file that is not readable by javafx (gsm format)), my only alternative to this is to open a new undecorated and small browser windows with the quiktime plugin for this I need or open the URL with this conditions or call a javascript method.

any suggestions???
  • 1. Re: How to open browser or execute Javascript from Javafx app
    shakir.gusaroff Expert
    Currently Being Moderated
    If your application is embedded into a web page , you can open a new document as:
      Application.getHostServices().showDocument(url);
    From java you can execute a javascript command like:
    JSObject jsWin = getHostServices().getWebContext();
     if (jsWin != null) {
         jsWin.eval(//Javascript command )
      }
      
  • 2. Re: How to open browser or execute Javascript from Javafx app
    992312 Newbie
    Currently Being Moderated
    this doesnt seems to work,

    gethostservices is not found, and Application. doesnt have getHost services.

    my javafx app runs in browser mode.

    Im trying this from a controller...

    Edited by: 989309 on Mar 4, 2013 4:42 PM
  • 3. Re: How to open browser or execute Javascript from Javafx app
    James_D Guru
    Currently Being Moderated
    You'll need to somehow arrange for your controller to have access to the HostServices object provided by Application.getHostServices(). Something like:
    public class MyController {
      private HostServices hostServices ;
      
      // usual controller stuff... FXML injected fields, initialize method, etc
    
      public void setHostServices(HostServices hostServices) {
        this.hostServices = hostServices ;
      }
    
      // event handler method which shows web page
      @FXML
      protected void handleSomeButton() {
        hostServices.showDocument(...);
      }
    }
    and then in your application class
    public class MyApp extends Application {
      @Override
      public void start(Stage primaryStage) {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("path/to/fxml/file"));
        Object root = loader.load();
        MyController controller = loader.<MyController>getController();
        controller.setHostServices(this.getHostServices());
        /// usual scene setup, etc
      }
      public static void main(String[] args) { launch(args); }
    }
    Note this doesn't let you access the host services in the controller's initialize method, as that method is invoked before you call controller.setHostServices(...). If you to access the host services on startup, you'll need to tinker with the code organization a little more.
  • 4. Re: How to open browser or execute Javascript from Javafx app
    shakir.gusaroff Expert
    Currently Being Moderated
    Hi. Try the following:


      
    package jsobjectsample;
    
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    
    public class JSObjectSample extends Application {
        
        @Override
        public void start(Stage stage) throws Exception {
            Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));
            
            Scene scene = new Scene(root);
            
            stage.setScene(scene);
            stage.show();
        }
    
      
        public static void main(String[] args) {
            launch(args);
        }
    }
    //Sample.fxml
    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import java.lang.*?>
    <?import java.util.*?>
    <?import javafx.scene.*?>
    <?import javafx.scene.control.*?>
    <?import javafx.scene.layout.*?>
    <?import javafx.scene.web.*?>
    
    <AnchorPane id="AnchorPane" prefHeight="200.0" prefWidth="320.0" xmlns:fx="http://javafx.com/fxml" fx:controller="jsobjectsample.SampleController">
      <children>
        <Button fx:id="button" layoutX="126.0" layoutY="90.0" onAction="#handleButtonAction" text="Open url" />
        <WebView layoutX="78.0" layoutY="118.0" prefHeight="750" prefWidth="550" fx:id = "wv" />
      </children>
    </AnchorPane>
    package jsobjectsample;
    
    import java.net.URL;
    import java.util.ResourceBundle;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.control.Label;
    import javafx.scene.web.WebEngine;
    import javafx.scene.web.WebView;
    
    
    public class SampleController implements Initializable {
        
        
        @FXML
        private WebView wv;
     
        
        @FXML
        private void handleButtonAction(ActionEvent event) {
                 
            WebEngine webEngine = wv.getEngine();
            webEngine.load("http://www.oracle.com/products/index.html");
    
                }
        
        @Override
        public void initialize(URL url, ResourceBundle rb) {
                   
           
        }    
    }
    You can get the JSObject as
    JSObject win = (JSObject) webEngine.executeScript("//------“);
  • 5. Re: How to open browser or execute Javascript from Javafx app
    992312 Newbie
    Currently Being Moderated
    I think webengine wont support plugins right??
  • 6. Re: How to open browser or execute Javascript from Javafx app
    992312 Newbie
    Currently Being Moderated
    Ok, this is working at least for a basic thing, I've put HostedServices on a static variable and I access from a common method from any controller now.

    I made a simple test wint an alert and worked, I will try somehting more advanced later to see if it works as expected.
  • 7. Re: How to open browser or execute Javascript from Javafx app
    James_D Guru
    Currently Being Moderated
    Did you try my approach? I prefer to inject the values needed into controllers, as I don't like the controller to rely on an API provided by the application. You may find it more flexible in the long run.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points