How can I make a Vaadin Notifiaction appear instantly

Viewed 135

So I have a method createDataOffer() which takes a couple of seconds to finish and I want to let the user know the system is doing something by showing a notificaion upon clicking the button.

However the notificaion appears only after the method has finnished. So how can I make the notification appear instantly? I allready tried adding two separate click listeners for the respective calls, but same result.

   Button createButton = new Button("Create");
   Notification startNotification = new Notification("Creating data offer", 3000);
    createButton.addClickListener(evt -> {
        startNotification.open();
        createDataOffer(price);
    });
3 Answers

Similar to André Schild's answer, there is a thread on the Vaadin forum that might help you.

This Code snippet is mentioned in the thread:

Button testButton = new Button("Test UI access", click -> {
Thread backgroundThread = new Thread(() -> {
    // anything happening inside this thread will not be updated to the ui automatically, and needs to be pushed to the ui.
    Thread.sleep(2000); // wait 2 seconds to imitate long task
    this.ui.access(() -> {
        Notification.show("Test notification");
    });
}
});

Furthermore @Push annotation is also mentioned in this thread and might help you.

Best would be to have the long running taks as a separate thread, then your UI update will show up instantly.

If you wish to update the UI from the long running thread, you have to use something like this:

ui.access(() -> statusLabel.setText(statusText));

https://vaadin.com/docs/flow/advanced/tutorial-push-access.html

try the following

   Button createButton = new Button("Create");
   Notification startNotification = new Notification("Creating data offer", 3000);
   createButton.addClickListener(evt -> {
       startNotification.open();
       new Thread(() -> createDataOffer(price)).start();
   });

This way the notification will be shown as soon as the createButton is clicked.

Related