在JavaFX中在后台执行任务

时间:2012-02-06 18:32:11

标签: service task javafx-2

我想在TilePane中加载最多九个面板。对于每个窗格,我必须运行内容的计算(大约300ms),然后必须构建Panel(大约500ms)。我想要的是,有九个ProgressIndicator在计算后与每个面板交换。 我尝试使用Platform.runlater命令以及服务类。结果总是一样的。显示了ProgressIndicator,但未显示动画。几秒钟后,所有面板都会立刻出现。 有没有可能,指标是动画的时间,我可以一个接一个地交换它们?

3 个答案:

答案 0 :(得分:12)

JavaFX具有用于UI事件的事件调度线程。所有使用UI的工作都应该在这个线程上进行。并且不应该在那里进行非UI计算以避免UI中的滞后。

见下一段代码:

public class Indicators extends Application {

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

    @Override
    public void start(Stage stage) {
        Pane root = new HBox();
        stage.setScene(new Scene(root, 300, 100));

        for (int i = 0; i < 10; i++) {
            final ProgressIndicator pi = new ProgressIndicator(0);
            root.getChildren().add(pi);

            // separate non-FX thread
            new Thread() {

                // runnable for that thread
                public void run() {
                    for (int i = 0; i < 20; i++) {
                        try {
                            // imitating work
                            Thread.sleep(new Random().nextInt(1000));
                        } catch (InterruptedException ex) {
                            ex.printStackTrace();
                        }
                        final double progress = i*0.05;
                        // update ProgressIndicator on FX thread
                        Platform.runLater(new Runnable() {

                            public void run() {
                                pi.setProgress(progress);
                            }
                        });
                    }
                }
            }.start();
        }

        stage.show();

    }
}

答案 1 :(得分:12)

这就是我解决问题的方法:

import java.util.Random;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.concurrent.Worker;
import javafx.concurrent.Worker.State;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.TilePane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;


public class Minimal extends Application {

private TilePane loadPane;
private ProgressIndicator[] indicators = new ProgressIndicator[9];
private Label loading[] = new Label[9];
private Color[] colors = {Color.BLACK,Color.BLUE,Color.CRIMSON,Color.DARKCYAN,Color.FORESTGREEN,Color.GOLD,Color.HOTPINK,Color.INDIGO,Color.KHAKI};
private int counter = 0;

@Override
public void start(Stage primaryStage) throws Exception {
    //creating Layout
    final Group root = new Group();                             
    Scene scene = new Scene(root, 400, 400);            
    primaryStage.setScene(scene);
    primaryStage.setResizable(false);
    StackPane waitingPane = new StackPane();
    final ProgressBar progress = new ProgressBar();
    Label load = new Label("loading things...");
    progress.setTranslateY(-25);
    load.setTranslateY(25);
    waitingPane.getChildren().addAll(new Rectangle(400,400,Color.WHITE),load,progress);
    root.getChildren().add(waitingPane);

    //Task for computing the Panels:
    Task<Void> task = new Task<Void>() {
        @Override
        protected Void call() throws Exception {
            for (int i = 0; i < 20; i++) {
                try {
                    Thread.sleep(new Random().nextInt(1000));
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
                final double prog = i*0.05;
                Platform.runLater(new Runnable() {
                    public void run() {
                        progress.setProgress(prog);
                    }
                });
            }
            return null;
        }
    };

    //stateProperty for Task:
    task.stateProperty().addListener(new ChangeListener<Worker.State>() {

        @Override
        public void changed(ObservableValue<? extends State> observable,
                State oldValue, Worker.State newState) {
            if(newState==Worker.State.SUCCEEDED){
                loadPanels(root);
            }
        }
    });

    //start Task
    new Thread(task).start();

    primaryStage.show();
}

private void loadPanels(Group root) {
    //change to loadPanel:
    root.getChildren().set(0,createLoadPane());

    //Service:
    final Service<Rectangle> RecBuilder = new Service<Rectangle>() {
          @Override protected Task<Rectangle> createTask() {
              return new Task<Rectangle>() {
                  @Override protected Rectangle call() throws InterruptedException {
                      updateMessage("loading rectangle . . .");
                      updateProgress(0, 10);
                      for (int i = 0; i < 10; i++) {
                        Thread.sleep(100);
                      }
                      updateMessage("Finish!");
                      return new Rectangle((380)/3,(380)/3,colors[counter]);
                  }
              };
          }
    };

    //StateListener
    RecBuilder.stateProperty().addListener(new ChangeListener<Worker.State>() {
        @Override public void changed(ObservableValue<? extends Worker.State> observableValue,
                        Worker.State oldState, Worker.State newState) {
            switch (newState) {
            case SCHEDULED:
                break;
            case READY:
            case RUNNING:
                break;
            case SUCCEEDED:
                Rectangle rec = RecBuilder.valueProperty().getValue();
                indicators[counter].progressProperty().unbind();
                loading[counter].textProperty().unbind();
                loadPane.getChildren().set(counter, rec);
                if(counter<8){
                    counter++;
                    nextPane(RecBuilder);
                }
                break;
            case CANCELLED:
            case FAILED:
                loading[counter].textProperty().unbind();
                loading[counter].setText("Failed!");
                if(counter<8){
                    counter++;
                    nextPane(RecBuilder);
                }
                break;
            }
        }
    });

    //begin PanelBuilding:
    nextPane(RecBuilder);
}

private void nextPane(Service<Rectangle> recBuilder) {
    loading[counter].textProperty().bind(recBuilder.messageProperty());
    indicators[counter].visibleProperty().bind(recBuilder.progressProperty().isNotEqualTo(new SimpleDoubleProperty(ProgressBar.INDETERMINATE_PROGRESS)));
    recBuilder.restart();
}

private Node createLoadPane() {
    loadPane = new TilePane(5,5);
    loadPane.setPrefColumns(3);
    loadPane.setPadding(new Insets(5));
    for(int i=0;i<9;i++){
        StackPane waitingPane = new StackPane();
        Rectangle background = new Rectangle((380)/3, (380)/3, Color.WHITE);
        indicators[i] = new ProgressIndicator();
        indicators[i].setPrefSize(50, 50);
        indicators[i].setMaxSize(50, 50);
        indicators[i].setTranslateY(-25);
        indicators[i].setTranslateX(-10);
        loading[i] = new Label();
        loading[i].setTranslateY(25);
        waitingPane.getChildren().addAll(background,indicators[i],loading[i]);
        loadPane.getChildren().add(waitingPane);
    }

    return loadPane;
}

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

}

答案 2 :(得分:1)

最简单的方法是使用lambda和线程。

private void logIn() {
    new Thread(() -> {
        //CONNECT TO WEB AND LOG IN (ON OUT OF FX THREAD)
        boolean loginSuccess = new LogIn(emial.getText(), pass.getText()).execute();

        //DO SOMETHING WITH CONTROLLS ON FX THREAD ACCORDING RESULT OF OVER
        Platform.runLater(() -> {
            if (loginSuccess) {
                info.setText("SUCCESS");
                info.setTextFill(Color.web("#268515"));
            } else {
                info.setText("FAIL");
                info.setTextFill(Color.web("#CD000E"));
            }
        });
    }).start();
}