Question

I'm trying to get my head around a concurrency problem. When I click a button to login to my system I perform a check to see whether the user have correct privileges to log in, if they do my data model is populated and the user is forwarded to the 'Dashboard'.

In order to improve the HCI of my UI, I want to toggle the show and hide of my spinner UI element, so the user doesn't think that the system has frozen whilst background tasks are running.

I am currently handling this login script on a Runnable:

        loginBtn.setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent mouseEvent) {

            // Try to verify the user with the provided credentials
            // run this on a new thread as it takes 5-10 seconds to compute
            new Thread(new Runnable() {
                @Override
                public void run() {

                    // Set the credentials to pass
                    String username = txtUsername.getText();
                    String password = txtPassword.getText();

                    if(verifyUser(username, password)){

                        // Set the loggedIn variable to true (singleton db connection)
                        ScreensFramework.connected = true;

                        // Check we build the system successfully (returns boolean)
                        if(ScreensFramework.authenticated()){
                            hideSpinner();
                            goToDashboard(new ActionEvent());
                        }
                    } else {
                        // there was an error with the connection
                        hideSpinner();
                        setError(error);
                    }
                }
            }).start();
        }
    });

The hideSpinner() method is simply:

    private void hideSpinner(){
       spinnerWrap.setVisible(false);
    }

the problem I am having, is that I cannot call hideSpinner() in my runnable, why is this? and how could I go about getting the component to update whilst the thread runs.

Thanks in advance, Alex.

Was it helpful?

Solution

For your current setup, use runLater to manipulate your UI on the JavaFX application thread.

Platform.runLater(()->spinnerWrap.setVisible(false));

You should use a Task for your off JavaFX thread logic, then, rather than using runLater, you could bind the spinner visible property to the task running property.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top