带有fxml的JavaFX和ProgressIndicator

时间:2017-11-08 17:40:40

标签: java javafx

我从早上开始想知道如何解决这个问题。我有登录申请。当用户等待登录时,我想使用processindicator。我使用了第二个线程,但它不起作用

主装载机fxml

MainController

@FXML public StackPane MainStackPane;

    public void initialize(URL arg0, ResourceBundle arg1) {

    FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/LoginForm/Login.fxml"));
    Pane pane = null;

    try {
        pane = loader.load();
    } catch (IOException e) {System.out.println(e.getMessage());}

    LoginController login = loader.getController();
    login.setMainController(this);

    setScreen(pane, true);
}

public void setScreen(Pane pane, boolean clean){

    MainStackPane.getChildren().addAll(pane);
    }

LoginForm的:

private MainController mainController;
private void Zaloguj() throws IOException {
    String cryptUser = null, cryptPass = null;

    Test test = new Test(this.mainController);
    test.start();

    try {
        Thread.sleep(5000);
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    LoginSQL sql = new LoginSQL();`
    Byte LoginResult = sql.SQLselect(cryptUser, cryptPass);

    ...}

班级考试

public class test extends Service<Void>{

private MainController mainController;

public test(MainController mainController) {
    this.mainController = mainController;
}

protected Task<Void> createTask() {
    return new Task<Void>() {
        @Override
        protected Void call() throws Exception {
            System.out.println("Service: START");

            ProgressIndicator p = new ProgressIndicator(); 

                     Platform.runLater(new Runnable() {
                         @Override public void run() {

                    mainController.MainStackPane.getChildren().addAll(p);
                     }});

                if (isCancelled()) {
                    mainController.MainStackPane.getChildren().remove(p);
                }
                return null;
            };};}}  

ProgressIndicator仅在登录后出现在下一个窗口中。怎么做?

1 个答案:

答案 0 :(得分:1)

JavaFX渲染仅在主线程中发生。如果添加Link然后使用ProgressIndicator,则JavaFX将在Thread.sleep()完成之前不呈现指示符。此外,如果登录请求挂起,JavaFX也将等到登录请求完成后再渲染。

您需要做的是确保永不中断/挂起主线程。删除Thread.sleep(),并将您的登录请求移至子线程。请求完成后,通知主线程,以便它可以删除Thread.sleep