加载JavaFX任务的逻辑

时间:2014-04-02 07:04:11

标签: javafx javafx-2 javafx-8

我想在JavaFX Task中添加额外的逻辑。我使用此代码从网络服务器加载Java对象。

    ...........
    Tab tabdata = new Tab();
                            Task<VBox> task = createLoadImagesTask(data);
                            StackPane stack = createVeiledPane(task, 120, 120);
                            runTask(task);
                            tabdata.setContent(stack);
                            AgentsDataStage.addNewTab(tabdata, newValue.getValue().toString());
    ............


private static Task<VBox> createLoadImagesTask(final InternalData data)
    {
        return new Task<VBox>()
        {
            @Override
            protected VBox call() throws Exception
            {

                    TabContent contentc = new TabContent();
                    return contentc.initTestTabContentData();

            }
        };
    }

    private static void runTask(Task<? extends Node> task)
    {
        Thread taskThread = new Thread(task, "Task Runner");
        taskThread.setDaemon(true);
        taskThread.start();
    }

    private static StackPane createVeiledPane(Task<? extends Node> task, double prefWidth, double prefHeight)
    {
        Region veil = new Region();
        veil.setStyle("-fx-background-color: transparent");
        veil.setPrefSize(prefWidth, prefHeight);
        ProgressIndicator progress = new ProgressIndicator();
        double progressSize = Math.min(prefWidth, prefHeight);
        clampSize(progress, progressSize * 0.75);
        progress.progressProperty().bind(task.progressProperty());
        StackPane stack = new StackPane();
        stack.getChildren().setAll(veil, progress);

        task.setOnSucceeded(new EventHandler<WorkerStateEvent>()
        {
            @Override
            public void handle(WorkerStateEvent t)
            {
                stack.getChildren().setAll(task.getValue());
            }
        });

        return stack;
    }

    private static void clampSize(Control control, double size)
    {
        control.setMinSize(ProgressIndicator.USE_PREF_SIZE, ProgressIndicator.USE_PREF_SIZE);
        control.setPrefSize(size, size);
        control.setMaxSize(ProgressIndicator.USE_PREF_SIZE, ProgressIndicator.USE_PREF_SIZE);
    }

我想在任务花费10秒以上显示警告消息时添加额外的逻辑。并且如果任务未能再次显示警告消息。你能帮我改进代码吗?

0 个答案:

没有答案