繁体   English   中英

JavaFX进度指示器不可见

[英]JavaFX Progress Indicator Not Visible

嗨,我正在编写一个自定义登录对话框。 我想在验证用户凭据的同时显示进度指示器。 当我运行按钮处理程序时,即使我调用setVisible(true)方法,进度指示器也不会出现。 文本也不可见。

    @FXML
private void handleLoginButton2(ActionEvent event) {
    //username and password required
    if (userNameTextField.getText().isEmpty() || passwordTextField.getText().isEmpty()) {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle("Login Validation");
        alert.setHeaderText("Validation Failed");
        String msg = "Please input user name and password.";
        alert.setContentText(msg);
        alert.showAndWait();
    } else {
        try {
            loginProgressIndicator.setVisible(true);
            loginStatusText.setText("Authorising...");
            Class.forName(JDBC_DRIVER2);
            String user = userNameTextField.getText();
            String password = passwordTextField.getText();
            Connection conn = DriverManager.getConnection(JDBC_URL2, user, password);
            loginStatusText.setText("Authorisation Successful");
            loginProgressIndicator.setVisible(false);
            //close login window
            windowManager.closeLoginWindow();
            //open main screen
            windowManager.showMainView();
        } catch (ClassNotFoundException | SQLException | IOException ex) {
            Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
            loginStatusText.setText("Authorisation Failed");
            loginProgressIndicator.setVisible(false);
        }
    }
}

从运行在单独线程中的任务进行数据库访问,并在onSucceeded / onFailed事件处理程序中处理结果:

public class AuthorizeTask extends Task<Void> {

    private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    private static final String JDBC_URL = "jdbc:mysql://localhost:3306/victorydb?zeroDateTimeBehavior=convertToNull";
    private final String user;
    private final String password;

    public AuthorizeTask(String user, String password) {
        if (user == null || password == null) {
            throw new IllegalArgumentException();
        }
        this.user = user;
        this.password = password;

    }

    @Override
    protected Void call() throws Exception {
        Class.forName(JDBC_DRIVER);
        Connection conn = DriverManager.getConnection(JDBC_URL, this.user, this.password);

        return null;
    }

}
Task<Void> task = new AuthorizeTask(userNameTextField.getText(), passwordTextField.getText());

task.setOnFailed(evt -> {
    Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, task.getException());
    loginStatusText.setText("Authorisation Failed");
    loginProgressIndicator.setVisible(false);
});
task.setOnSucceeded(evt -> {
    // you may need to add a try/catch here

    loginStatusText.setText("Authorisation Successful");
    loginProgressIndicator.setVisible(false);
    //close login window
    windowManager.closeLoginWindow();
    //open main screen
    windowManager.showMainView();
});

loginProgressIndicator.setVisible(true);
loginStatusText.setText("Authorising...");

// run this on background thread to avoid blocking the application thread
new Thread(task).start();

不过,您可能想将Task的type参数更改为Connection ,因为您稍后可能需要使用Connection

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM