JavaFX WebView display website via URL

Description

JavaFX WebView display website via URL

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class Main extends Application {
   public static void main(String[] args) {
      Application.launch(args);/*  w ww. j  a va 2s. c  o  m*/
   }

   @Override
   public void start(Stage primaryStage) {
      VBox vb = new VBox();
      vb.setId("root");

      WebView browser = new WebView();
      WebEngine engine = browser.getEngine();
      String url = "https://demo2s.com/";
      engine.load(url);

      vb.setPadding(new Insets(30, 50, 50, 50));
      vb.setSpacing(10);
      vb.setAlignment(Pos.CENTER);
      vb.getChildren().addAll(browser);

      Scene scene = new Scene(vb);
      primaryStage.setScene(scene);
      primaryStage.show();
   }
}



PreviousNext

Related