gpt4 book ai didi

java - 为什么该控件在后台线程执行时不更新其内容?

转载 作者:行者123 更新时间:2023-12-02 12:23:20 25 4
gpt4 key购买 nike

taResults 是一个 TextArea 控件。该线程运行两秒钟,但与此同时 TextArea 控件永远不会更新。当线程完成时,taResults 会更新。

    private void ProcessSchema() {
Task<Void> runnable = new Task<Void>() {
public Void call() {
try {Thread.sleep(2000);} catch (InterruptedException e) {}
return null;
}
};
try {
long startTime = System.currentTimeMillis();
// This text never appears in the TextArea control
taResults.setText("working...");
Thread thread = new Thread(runnable);
thread.start();
while (true) {
if (thread.getState() == Thread.State.TERMINATED) {
break;
}
}
final long endTime = System.currentTimeMillis();
// This text does appear in the control!
taResults.setText("Total execution time: " + ((double)(endTime - startTime))/1000. + " seconds.");
} catch (Exception e) {
System.out.println(e.getLocalizedMessage());
}
}

最佳答案

您的 busy-while 循环正在 FX 应用程序线程上执行,从而阻止它执行其应该执行的操作(更新 UI 等)。如果您想在 Task 完成时执行某些操作,可以使用其 onSucceeded处理程序:

private void ProcessSchema() {
Task<Void> runnable = new Task<Void>() {
public Void call() throws Exception {
Thread.sleep(2000)
return null;
}
};

final long startTime = System.currentTimeMillis();

runnable.setOnSucceeded(e -> {
final long endTime = System.currentTimeMillis();
taResults.setText("Total execution time: " + ((double)(endTime - startTime))/1000. + " seconds.");
});

// if needed to can also do this,
// which gets executed if the task terminates with an exception:
runnable.setOnFailed(e -> {
runnable.getException().printStackTrace();
});

taResults.setText("working...");
Thread thread = new Thread(runnable);
thread.start();

}

这是一个完整的示例:

import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class TaskExample extends Application {

private TextArea taResults ;
private Button startButton;

@Override
public void start(Stage primaryStage) {
taResults = new TextArea();
taResults.setEditable(false);
startButton = new Button("Start");
startButton.setOnAction(e -> processSchema());

BorderPane root = new BorderPane(taResults);
root.setBottom(startButton);
BorderPane.setAlignment(startButton, Pos.CENTER);
BorderPane.setMargin(startButton, new Insets(5));

primaryStage.setScene(new Scene(root));
primaryStage.show();
}

private void processSchema() {
Task<Void> runnable = new Task<Void>() {
public Void call() throws Exception {
Thread.sleep(2000);
return null;
}
};

final long startTime = System.currentTimeMillis();

runnable.setOnSucceeded(e -> {
final long endTime = System.currentTimeMillis();
taResults.setText("Total execution time: " + ((double)(endTime - startTime))/1000. + " seconds.");
startButton.setDisable(false);
});

// if needed to can also do this,
// which gets executed if the task terminates with an exception:
runnable.setOnFailed(e -> {
runnable.getException().printStackTrace();
});

taResults.setText("working...");
startButton.setDisable(true);
Thread thread = new Thread(runnable);
thread.start();

}

public static void main(String[] args) {
launch(args);
}
}

关于java - 为什么该控件在后台线程执行时不更新其内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45620460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com