Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set a JavaFX WebView as big as the scene?

I have a JavaFX WebView which I want to have as big as the scene on start. If I could make it resize with the scene, this would be perfect.
But right now I focus just on the initial size.

@Override
public void start(Stage stage) {
    try {
        Scene scene = new Scene(createWebViewPane(), 1920, 1080);
        scene.getStylesheets().add(getClass().getResource("Styles/application.css").toExternalForm());
        stage.setScene(scene);
        stage.show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

The above code creates a scene with width: 1920px, height: 1080px. createWebViewPane() creates a WebView on a pane.

private Pane createWebViewPane() {
    final Pane pane = new Pane();
    pane.minWidth(1920);
    pane.minHeight(1080);
    WebView browser = new WebView();
    browser.minWidth(1920);
    browser.prefWidth(1920);
    browser.minHeight(1080);
    browser.prefHeight(1080);
    WebEngine webEngine = browser.getEngine();
    webEngine.load(getClass().getResource("index.html").toExternalForm());
    pane.getChildren().add(browser);
    return pane;
}

There I try to set the width / height. I also set it in the stylesheet.

web-view{
    -fx-font-scale: 1;  
    -fx-min-width: 1920px;   
    -fx-min-height: 1080px;   
    -fx-pref-width: 100%; 
    -fx-pref-height: 100%;
}

But none of these properties seem to affect the WebView. It still has it's default size. 800px in width and 600px in height.

like image 713
Peter Avatar asked Nov 20 '15 09:11

Peter


People also ask

What is the difference between stage and scene JavaFX?

Stage , represents a window in a JavaFX desktop application. Inside a JavaFX Stage you can insert a JavaFX Scene which represents the content displayed inside a window - inside a Stage .

Can a canvas be used in a layout JavaFX?

Because the Canvas is a Node subclass, it can be used in the JavaFX scene graph.

What are the two parameters of stage in JavaFX divided as?

A stage has two parameters determining its position namely Width and Height. It is divided as Content Area and Decorations (Title Bar and Borders).


1 Answers

Add the WebView to a StackPane, which "will attempt to resize each child to fill its content area." Resize the frame to see how the StackPane reflows the WebView content based on the default Pos.CENTER.

image

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

/** @see http://stackoverflow.com/a/33824164/230513 */
public class WebViewPane extends Application {

    @Override
    public void start(Stage primaryStage) {
        StackPane root = new StackPane();
        WebView webView = new WebView();
        WebEngine webEngine = webView.getEngine();
        webEngine.load("http://www.example.com");
        root.getChildren().add(webView);
        Scene scene = new Scene(root);
        primaryStage.setTitle("WebView");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}
like image 196
trashgod Avatar answered Oct 19 '22 09:10

trashgod