Good evening! There is a code, the text should change after a pause, but an exception is thrown, why? How to write correctly?

public void initialize(URL fxmlFileLocation, ResourceBundle resources) { loginfield.setStyle("-fx-font: 25 calibri"); btn.setOnAction( event -> { if (loginfield.getText().equals(login)) { result.setText("Your login is right"); Thread.sleep(1000); result.setText("Logged in"); } else if (loginfield.getText().isEmpty()) { result.setText("You have typed nothing"); } else if (loginfield.getText() != login) { result.setText("Your login isn't right"); } } ); } 

    1 answer 1

    Apparently Kalbek is running in the ui stream, and you are trying to stop it, which is not very good.

    Try using java.util.Timer to perform a text change:

      new Timer().schedule(new TimerTask() { @Override public void run() { Platform.runLater(() -> result.setText("Logged in")); } }, TimeUnit.SECONDS.toMillis(1));