簡體   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