我正在尝试使用setOnSucceeded侦听器实现JavaFX Task。

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

07-27 19:59