Showing spinner

Hi guys,
I have misunderstanding about how correctly show spinner during action processing when button clicked.

Hi,

Are you running a long task in a background thread?

You’ll need to show the spinner before beginning the task and use polling or server push for the client to continuously request updates from the server. When the background task finished, you’ll need to hide the spinner using the access method since all UI operations need to use the main UI thread.

You can find more info on
ProgressBar
and
Polling
and
Push
in the docs.

The following example might be of help.

ProgressBar spinner = new ProgressBar();
spinner.setIndeterminate(true);
spinner.setVisible(false);
layout.addComponent(spinner);

layout.addComponent(new Button("Start long task", event -> {
    UI ui = UI.getCurrent();

    // Instruct client side to poll for changes and show spinner
    ui.setPollInterval(500);
    spinner.setVisible(true);

    // Start background task
    CompletableFuture.runAsync(() -> {

        // Do some long running task
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // Need to use access() when running from background thread
        ui.access(() -> {
            // Stop polling and hide spinner
            ui.setPollInterval(-1);
            spinner.setVisible(false);
        });
    });
}));

Adam Wagner, that’s what I need. Respect and thanks a lot!