#author("2018-05-12T14:00:06+00:00","default:haikikyou","haikikyou")
#contents

* ScrollPane [#hd65ed3b]

- コンテンツのスクロールおよびクリップされるビューポートを提供するコントロール

#ref(javafx-container-scrollpane.png)

&label(sample){サンプル};  指定されたURLのコンテンツを表示する

&ref(./SampleScrollPane.java);

#geshi(java){{{
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.getLoadWorker().cancel();
			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);
	}
}
}}}

&label(info){補足};
- VBoxでURLを入力するTextFieldとWebViewを表示するScrollPaneを配置している。
- WebViewはProgressBarで進捗表示する。BarはStackPaneでWebViewの上に表示する。
- WebViewは、Windowのリサイズで拡大・縮小するようにVBoxのsetVgrowとScrollPaneでsetFitToWidthとsetFitToHeightを呼んでいる。
- URLの変更するとWebViewのコンテンツもリロードするよう、setOnActionでEventListenerを登録している。
* 参考リンク [#w4a3d674]

- https://docs.oracle.com/javase/jp/8/javafx/api/javafx/scene/control/ScrollPane.html
- https://docs.oracle.com/javase/jp/8/javafx/user-interface-tutorial/scrollpane.htm


トップ   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
目次
ダブルクリックで閉じるTOP | 閉じる
GO TO TOP