我正在尝试使用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。你能帮我解决这个问题吗?
P.S
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;
}
}