gpt4 book ai didi

java - setOnSucceeded 未调用

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

我正在尝试使用 setOnSucceeded 监听器来实现 JavaFX 任务。

class GetDailySalesTask extends Task<ObservableList<VBox>>
{

@Override
protected ObservableList<VBox> call() throws Exception
{
TabContentInfrastructure content = new TabContentInfrastructure();
return FXCollections.observableArrayList(content.initTestTabContentData());
}
}

我在这里调用 Java 类:

VBox vb = new VBox();

Task<ObservableList<VBox>> task = new GetDailySalesTask();

Region veil = new Region();
veil.setStyle("-fx-background-color: rgba(0, 0, 0, 0.4)");
veil.setPrefSize(240, 160);
ProgressIndicator p = new ProgressIndicator();
p.setMaxSize(140, 140);

//p.progressProperty().bind(service.progressProperty());
veil.visibleProperty().bind(task.runningProperty());
p.visibleProperty().bind(task.runningProperty());
//vb.visibleProperty().bind(service.runningProperty().not());
//tableView.itemsProperty().bind(service.valueProperty());

StackPane stack = new StackPane();

System.out.println("service.valueProperty() = " + task.valueProperty());

task.setOnSucceeded(new EventHandler<WorkerStateEvent>()
{
@Override
public void handle(WorkerStateEvent t){

System.out.print("Yes**********" + t.getSource().getValue());
stack.getChildren().clear();
t.getSource().getValue();
stack.getChildren().addAll(task.getValue().get(0));


}
});



task.setOnFailed(new EventHandler<WorkerStateEvent>()
{
@Override
public void handle(WorkerStateEvent t){

System.out.print("Entered setOnFailed**********" + t.getSource().getValue());



}
});



stack.getChildren().addAll(vb, veil, p);

tabdata.setContent(stack);
new Thread(task).start();

由于某种原因,setOnSucceeded 没有被调用。你能帮我解决这个问题吗?

附注

public class TabContentInfrastructure
{

public VBox initTestTabContentData() throws InterruptedException
{

Thread.sleep(5000);
final VBox vbox = new VBox(0);
FlowPane flow = new FlowPane();
........................
vbox.getChildren().addAll(flow, screenController);
return vbox;
}
}

最佳答案

你必须明确这一点,

Javafx应用程序线程和工作线程完全不同。

您必须先阅读本教程。 docs

关于java - setOnSucceeded 未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21972375/

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