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.
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 .
Because the Canvas is a Node subclass, it can be used in the JavaFX scene graph.
A stage has two parameters determining its position namely Width and Height. It is divided as Content Area and Decorations (Title Bar and Borders).
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
.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With