[英]How to make progress bar for web pages loading process in JavaFX?
I am working on a JavaFX application which uses WebView
. 我正在使用
WebView
的JavaFX应用程序上工作。 I'd like to make a ProgressBar
which shows the user the progress of loading a page. 我想制作一个
ProgressBar
,向用户显示加载页面的进度。 I know how to create a ProgressBar
, and how to work with it, but I can't figure out how to get the progress of the WebView
/ WebEngine
. 我知道如何创建一个
ProgressBar
,以及如何使用它,但是我不知道如何获取WebView
/ WebEngine
。 How do I get the progress in order to update the ProgressBar
? 如何获取进度以更新
ProgressBar
?
You can do it like below code: 您可以按照以下代码进行操作:
Method to retrieve tasks using a background thread, java.util.Timer
or Future still work. 使用后台线程检索任务的方法,
java.util.Timer
或Future仍然有效。 But JavaFX
has a built-in API for doing such thing it's the Service
and Task classes (used ScheduleService
for recurring tasks). 但是
JavaFX
有一个内置的API来执行此操作,它是Service
和Task类(用于重复执行任务的ScheduleService
)。
// On the JavaFX thread. //在JavaFX线程上。
final Service<Foo> service = new Service<Foo> {
@Override
protected Task<Foo> createTask() {
return new Task<Foo>() {
@Override
protected Foo call() throws Exception {
// On another thread.
[...]
if (isCancelled()) {
updateMessage("Cancelled");
return null;
}
updateProgress(currentProgress++, totalProgress);
updateMessage("Now doing stuff");
[...]
return result;
}
};
}
};
progressBar.progressProperty().bind(service.progressProperty());
service.setOnSucceeded(event -> {
// On the JavaFX thread.
progressBar.progressProperty().unbind();
final Foo foo = (Foo)event.getSource().getValue();
// Pass the result to the view.
[...]
});
service.setOnCancelled(event -> {
// On the JavaFX thread.
progressBar.progressProperty().unbind();
[...]
});
service.setOnFailed(event -> {
// On the JavaFX thread.
progressBar.progressProperty().unbind();
final Throwable ex = event.getSource().getException();
// Log and show.
[...]
});
service.start();
The same Service can be reused several times (call either cancel() and start() or restart()) while a Task can only be used once (the Service will create a new Task each time it starts). 相同的Service可以重复使用多次(调用cancel()和start()或restart()),而Task只能使用一次(Service每次启动都会创建一个新Task)。
From the documentation of WebEngine
: 从
WebEngine
的文档中:
Loading always happens on a background thread.
加载始终发生在后台线程上。 Methods that initiate loading return immediately after scheduling a background job.
计划后台作业后,立即返回启动加载的方法。 To track progress and/or cancel a job, use the
Worker
instance available from thegetLoadWorker()
method.要跟踪进度和/或取消作业,请使用
getLoadWorker()
方法中可用的Worker
实例。
All you have to do is observe the progress
property of the Worker
. 您要做的就是观察
Worker
的progress
属性。 Here's a small example: 这是一个小例子:
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.Separator;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
WebView view = new WebView();
BorderPane root = new BorderPane(view, createTop(view), null, null, null);
primaryStage.setScene(new Scene(root, 1000, 600));
primaryStage.setTitle("WebView Example");
primaryStage.show();
}
private VBox createTop(WebView view) {
ProgressBar progBar = new ProgressBar();
progBar.progressProperty().bind(view.getEngine().getLoadWorker().progressProperty());
progBar.visibleProperty().bind(
Bindings.when(progBar.progressProperty().lessThan(0).or(progBar.progressProperty().isEqualTo(1)))
.then(false)
.otherwise(true)
);
progBar.managedProperty().bind(progBar.visibleProperty());
progBar.setMaxWidth(Double.MAX_VALUE);
VBox top = new VBox(5, createSearchBar(view), progBar, new Separator());
top.setAlignment(Pos.CENTER);
return top;
}
private HBox createSearchBar(WebView view) {
TextField urlField = new TextField("https://stackoverflow.com/");
view.getEngine().locationProperty().addListener(obs -> urlField.setText(view.getEngine().getLocation()));
Button loadBtn = new Button("Load");
loadBtn.setOnAction(event -> {
event.consume();
view.getEngine().load(urlField.getText());
});
loadBtn.disableProperty().bind(view.getEngine().getLoadWorker().runningProperty());
HBox searchBar = new HBox(5, urlField, new Separator(Orientation.VERTICAL), loadBtn);
searchBar.setPadding(new Insets(10, 10, 3, 10));
searchBar.setAlignment(Pos.CENTER);
HBox.setHgrow(urlField, Priority.ALWAYS);
return searchBar;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.