package sample; import javafx.application.Application; import javafx.concurrent.Worker.State; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ProgressBar; import javafx.scene.control.ScrollPane; import javafx.scene.control.TextField; import javafx.scene.layout.HBox; import javafx.scene.layout.Priority; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import javafx.scene.web.WebEngine; import javafx.scene.web.WebView; import javafx.stage.Stage; public class SampleScrollPane extends Application { /** * Initialize stage and start scene. * * @param primaryStage * @throws Exception */ @Override public void start(Stage primaryStage) throws Exception { final String homeUrl = "https://haikikyou.xsrv.jp/pwiki/"; VBox vbox = new VBox(); StackPane stackPane = new StackPane(); // Progress bar ProgressBar progress = new ProgressBar(); // Location field HBox controlArea = new HBox(); TextField locationField = new TextField(homeUrl); Button cancelButton = new Button("Cancel"); controlArea.getChildren().addAll(cancelButton, locationField); HBox.setHgrow(locationField, Priority.ALWAYS); // ScrollPane to show a WebView ScrollPane scrollPane = new ScrollPane(); // Create a WebView and load content from the specified url WebView webView = new WebView(); WebEngine engine = webView.getEngine(); engine.load(locationField.getText()); // Set WebView and Progress bar. stackPane.getChildren().addAll(webView, progress); progress.progressProperty().bind(engine.getLoadWorker().progressProperty()); engine.getLoadWorker() .stateProperty() .addListener((observable, oldState, newState) -> { if (newState == State.SUCCEEDED || newState == State.CANCELLED) { // Hide progress bar then page is ready or brank. progress.setVisible(false); } else if (newState == State.RUNNING) { // Show progress bar progress.setVisible(true); locationField.setText(engine.getLocation()); } }); scrollPane.setContent(stackPane); // Make webview fit ScrollPane view. scrollPane.setFitToWidth(true); scrollPane.setFitToHeight(true); vbox.getChildren().addAll(controlArea, scrollPane); // Grow the WebView when the window resized. VBox.setVgrow(scrollPane, Priority.ALWAYS); // Load webpage when the location text changed. locationField.setOnAction(event -> { engine.load(locationField.getText()); }); // Cancel loading. cancelButton.setOnAction(event -> { engine.getLoadWorker().cancel(); }); // Create a scene Scene scene = new Scene(vbox, 800, 800); primaryStage.setScene(scene); // Show window. primaryStage.show(); } /** * Launch application. * * @param args */ public static void main(String[] args) { launch(args); } }