Reputation: 477
For some reason components are always changed from button click simultaneously.
I need to change them one by one. Here is the example:
countButton.addClickListener(buttonClickEvent -> {
int input = Integer.parseInt(inputTextField.getValue());
long factorialResult = countFactorial(input);
resultFactorialLabel.setText("Factorial: " + factorialResult);
//just for example, could be processing that takes really long
try {
sleep(1000);
} catch (InterruptedException ignored) {
}
int fibonacciResult = countFibonacci(input);
resultFibonacciLabel.setText("Fibonacci: " + fibonacciResult);
});
When I run it - 10 seconds passes, then resultFactorialLabel and resultFibonacciLabel change simultaneously, when I want resultFactorialLabel to be changed and after that, in 10 seconds, resultFibonacciLabel to be changed. How can I do it?
And how is it made so, that labels change simultaneously?
Vaadin version: 10.0.1
Upvotes: 2
Views: 235
Reputation: 2749
This is the usual lifecycle:
So if you do a sleep
within your listener, it will just delay the response to the client.
If you need to do an expensive operation triggered by the button click, you should do it in a background thread and update the UI once the operation is complete. See docs for how to update UI from a background thread.
Upvotes: 2