Multithreading Javafx多个GUI线程-进度指示器
在JavaFXGUI中,进程在单独的线程上完成。不允许将进度指示器放在带有服务和任务的后台线程上,因为它不是FX线程,指示器是FX元素。可以在javafx中创建多个gui线程吗 或者,在加载其他gui元素时,是否有其他方法可以使进度指示器继续运行?当前它开始滚动,然后停止,直到加载窗格Multithreading Javafx多个GUI线程-进度指示器,multithreading,user-interface,javafx,progress-indicator,Multithreading,User Interface,Javafx,Progress Indicator,在JavaFXGUI中,进程在单独的线程上完成。不允许将进度指示器放在带有服务和任务的后台线程上,因为它不是FX线程,指示器是FX元素。可以在javafx中创建多个gui线程吗 或者,在加载其他gui元素时,是否有其他方法可以使进度指示器继续运行?当前它开始滚动,然后停止,直到加载窗格 @FXML public void budgetShow(ActionEvent event) { progressIndicator = new ProgressIndicator(-1.0);
@FXML
public void budgetShow(ActionEvent event) {
progressIndicator = new ProgressIndicator(-1.0);
rootPane.getChildren().add(progressIndicator);
progressIndicator.setVisible(true);
progressIndicator.toFront();
threadBudgetShow().start();
}
public Service<Void> threadBudgetShow() {
Service<Void> service = new Service<Void>() {
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@Override
protected Void call() throws Exception {
// Background Thread operations.
final CountDownLatch latch = new CountDownLatch(1);
Platform.runLater(new Runnable() {
@Override
public void run() {
try {
// FX Thread opeartions.
// budgetAnchorPane - reload.
if (budgetAnchorPane == null || !budgetAnchorPane.isVisible()) {
budgetAnchorPane = new BudgetAnchorPane();
rootPane.getChildren().add(budgetAnchorPane);
budgetAnchorPane.setVisible(true);
budgetAnchorPane.getChildren().remove(budgetAnchorPane.budgetTypeComboBox);
budgetAnchorPane.budgetTypeComboBox = new BudgetTypeCombobox();
budgetAnchorPane.getChildren().add(budgetAnchorPane.budgetTypeComboBox);
}
} finally {
rootPane.getChildren().remove(progressIndicator);
latch.countDown();
}
}
});
latch.await();
// Other background Thread operations.
return null;
}
};
}
};
return service;
}
@FXML
公共作废预算展示(ActionEvent事件){
progressIndicator=新的progressIndicator(-1.0);
rootPane.getChildren().add(progressIndicator);
progressIndicator.setVisible(true);
progressIndicator.toFront();
threadBudgetShow().start();
}
公共服务threadBudgetShow(){
服务=新服务(){
@凌驾
受保护的任务createTask(){
返回新任务(){
@凌驾
受保护的Void调用()引发异常{
//后台线程操作。
最终倒计时闩锁=新倒计时闩锁(1);
Platform.runLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
//外汇线程操作。
//预算窗格-重新加载。
如果(budgetAnchorPane==null | |!budgetAnchorPane.isVisible()){
budgetAnchorPane=新budgetAnchorPane();
rootPane.getChildren().add(budgetAnchorPane);
budgetAnchorPane.setVisible(true);
budgetAnchorPane.getChildren().remove(budgetAnchorPane.budgetTypeComboBox);
budgetAnchorPane.budgetTypeComboBox=新的budgetTypeComboBox();
budgetAnchorPane.getChildren().add(budgetAnchorPane.budgetTypeComboBox);
}
}最后{
rootPane.getChildren().remove(progressIndicator);
倒计时();
}
}
});
satch.wait();
//其他后台线程操作。
返回null;
}
};
}
};
回程服务;
}
不确定的进度指标
进度指标继续运行
我想你的意思是一个
默认情况下,进度指示器以不确定状态启动,您可以通过将其进度设置为不确定状态,随时将指示器更改回不确定状态:
progressIndicator.setProgress(ProgressIndicator.INDETERMINATE);
不确定的进度指标和任务 由于默认情况下进度是不确定的,如果在任务完成之前不更新任务的进度,则在任务运行时绑定到任务进度的进度指示器将保持不确定 样本 此示例中的进度指示器将只是一组旋转的点,指示任务完成前的不确定进度
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class ProgressTracker extends Application {
final int N_SECS = 10;
@Override
public void start(Stage stage) throws Exception {
Task task = createTask();
stage.setScene(
new Scene(
createLayout(
task
)
)
);
stage.show();
new Thread(task).start();
}
private Task<Void> createTask() {
return new Task<Void>() {
@Override public Void call() {
for (int i=0; i < N_SECS; i++) {
if (isCancelled()) {
break;
}
// uncomment updateProgress call if you want to show progress
// rather than let progress remain indeterminate.
// updateProgress(i, N_SECS);
updateMessage((N_SECS - i) + "");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
return null;
}
}
updateMessage(0 + "");
updateProgress(N_SECS, N_SECS);
return null;
}
};
}
private HBox createLayout(Task task) {
HBox layout = new HBox(10);
layout.getChildren().setAll(
createProgressIndicator(task),
createCounter(task)
);
layout.setAlignment(Pos.CENTER_RIGHT);
layout.setPadding(new Insets(10));
return layout;
}
private ProgressIndicator createProgressIndicator(Task task) {
ProgressIndicator progress = new ProgressIndicator();
progress.progressProperty().bind(task.progressProperty());
return progress;
}
private Label createCounter(Task task) {
Label counter = new Label();
counter.setMinWidth(20);
counter.setAlignment(Pos.CENTER_RIGHT);
counter.textProperty().bind(task.messageProperty());
counter.setStyle("-fx-border-color: forestgreen;");
return counter;
}
public static void main(String[] args) {
launch(args);
}
}
导入javafx.application.application;
导入javafx.concurrent.Task;
导入javafx.geometry.*;
导入javafx.scene.scene;
导入javafx.scene.control.*;
导入javafx.scene.layout.HBox;
导入javafx.stage.stage;
公共类ProgressTracker扩展了应用程序{
最终积分N_秒=10;
@凌驾
public void start(Stage)引发异常{
任务Task=createTask();
第二阶段(
新景(
创建布局(
任务
)
)
);
stage.show();
新线程(任务).start();
}
私有任务createTask(){
返回新任务(){
@重写公共无效调用(){
对于(int i=0;i
不确定的进度指标
进度指标继续运行
我想你的意思是一个
默认情况下,进度指示器以不确定状态启动,您可以通过将其进度设置为不确定状态,随时将指示器更改回不确定状态:
progressIndicator.setProgress(ProgressIndicator.INDETERMINATE);
不确定的进度指标