a vaadin countdown

Hi everyOne,

Sorry if I make some mistakes but english is not my mothertong.

I want to make a countdown as a CustomComponent. How can I do that? I tried several hours with thread, timer, timerTask…And it’s inconclusive. I want to make a really basic countdown with juste a label for example. is anyone has an idea? Or give me just a clue.

Thank you in advance.

The following helps to find the right solution:

@Push
public class MyVaadinUI extends UI
{

    @WebServlet(value = "/*", asyncSupported = true)
    @VaadinServletConfiguration(productionMode = false, ui = MyVaadinUI.class, widgetset = "my.vaadin.countdown.AppWidgetSet")
    public static class Servlet extends VaadinServlet {
    }

    @Override
    protected void init(VaadinRequest request) {
        final VerticalLayout layout = new VerticalLayout();
        layout.setMargin(true);
        setContent(layout);
        
        final ObjectProperty<Integer> counter = new ObjectProperty<Integer>(0);
        
        new Thread(new Runnable() {
            volatile int c = 1000;
            @Override
            public void run() {
                try {
                while( c > 0 ) {
                        UI.getCurrent().access(new Runnable() {
                            
                            @Override
                            public void run() {
                                counter.setValue(c);
                            }
                        });
                        Thread.sleep(1000);
                        c -= 1;
                }
                } catch(InterruptedException e) {}
            }
        }).start();

        Label label = new Label(counter);
        layout.addComponent(label);
    }
}

Thanks a lot!

So I have make it work but I need some explanation, why can’t I write:

while( c > 0 ) {
counter.setValue(c);
Thread.sleep(1000);
c -= 1;
}

I think there is some basically thing I don’t understand.

Thanks you for your help

Here is the explanation: https://vaadin.com/book/-/page/advanced.push.html#advanced.push.running