JavaFX进度指示器不可见

时间:2018-07-09 19:37:03

标签: javafx

您好,我正在编写一个自定义登录对话框。我想在验证用户凭据的同时显示进度指示器。当我运行按钮处理程序时,即使我调用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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

从运行在单独线程中的任务进行数据库访问,并在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的类型参数更改为Connection,因为稍后可能需要使用Connection ...