0

Can we add the JavaFX Webview into a container?

Actually I have a project in progress and I would like to have only one windows for ma UI. The "SplitPane" UI might be a good answer for me and functionalities I have to display. (see below)

In the "view" section, I would like to display a WebView, an particulary a map (MapBox or OpenStreetMap) doesn't matter !

Actually my map.html works in an independant window, I only need to know if what I want to do is possible, and if so, can you show me how to do it ?

For now, I have this class :

import java.io.IOException;

import javax.annotation.PostConstruct;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class MapViewer extends Application {

    @PostConstruct
    public static void main(String[] args) {

        launch(args);
    }

    public void start(Stage stage) {

        SplitPane root;

        try {
            root = (SplitPane)     FXMLLoader.load(getClass().getResource("Main.fxml"));

            Stage secondStage = new Stage();

            Scene scene = new Scene(root, 750, 300);

                scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());

            secondStage.setScene(scene);

            secondStage.setTitle("MapView");
            secondStage.sizeToScene();
            secondStage.show();

            Stage stage1 = new Stage();

            WebView webview = new WebView();

            webview.getEngine().load(MapViewer.class.getResource("/view  mapview3.html").toExternalForm());

            stage1.setTitle("MapView");
            stage1.setMaximized(true);
            stage1.setScene(new Scene(webview));
            webview.getEngine().setJavaScriptEnabled(true);

            stage1.show();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
Aditya
  • 3,080
  • 24
  • 47
stann
  • 1
  • `WebView` extends `Node` and therefore can be added as item to a `SplitPane` (there's a code snippet for doing this in the javadoc https://docs.oracle.com/javase/9/docs/api/javafx/scene/control/SplitPane.html) Moving the code for handling (some of) the content of the fxml to the application class instead of a controller class is a poor choice though... – fabian Oct 11 '18 at 11:44
  • Thanks for your answer. So for you, the best "choice" would be to generate the Scene in the main and then add the WebView (just like other components Label etc) in a controller ? – stann Oct 11 '18 at 12:19

0 Answers0