Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

java - Not update button title when start countdown

In my Vaadin project when click button then I need:

  1. Start countdown (10 seconds)
  2. Replace button's caption by left seconds (10,9,8 and so on)

I try this:

private Button buttonNext, buttonSendSMS;

private void runCountDown() {
        logger.info("countDown_start");
        buttonSendSMS.setEnabled(false);
        final Timer timer = new Timer();
        
        timer.scheduleAtFixedRate(new TimerTask() {
            int leftSeconds = 10;

            public void run() {
                leftSeconds--;

                UI.getCurrent().access(new Runnable() {

                    @Override
                    public void run() {
                        logger.info("leftSeconds = " + leftSeconds);
                        buttonSendSMS.setCaption(leftSeconds + "");
                    }
                });

                if (leftSeconds < 0) {
                    logger.info("countDown_finish");
                    timer.cancel();
                    buttonSendSMS.setEnabled(true);
                    new MessageService(getWzContext().getLang());
                    buttonSendSMS.setCaption(MessageService.getMessage("resend.sms"));
                }
            }
        }, 0, 1000);
    }

When click button here log:

 INFO] 12.01.2021 17:35:58.112 [Timer-0] com.myproject.client.ui.wz.SmsCodeStepWz$2$1.run(SmsCodeStepWz.java:202) 
    leftSeconds = 9
INFO] 12.01.2021 17:35:59.111 [Timer-0] com.myproject.client.ui.wz.SmsCodeStepWz$2$1.run(SmsCodeStepWz.java:202) 
    leftSeconds = 8
[INFO] 12.01.2021 17:36:00.110 [Timer-0] com.myproject.client.ui.wz.SmsCodeStepWz$2$1.run(SmsCodeStepWz.java:202) 
    leftSeconds = 7
[INFO] 12.01.2021 17:36:01.110 [Timer-0] com.myproject.client.ui.wz.SmsCodeStepWz$2$1.run(SmsCodeStepWz.java:202) 
    leftSeconds = 6
[INFO] 12.01.2021 17:36:02.111 [Timer-0] com.myproject.client.ui.wz.SmsCodeStepWz$2$1.run(SmsCodeStepWz.java:202) 
    leftSeconds = 5
[INFO] 12.01.2021 17:36:03.110 [Timer-0] com.myproject.client.ui.wz.SmsCodeStepWz$2$1.run(SmsCodeStepWz.java:202) 
    leftSeconds = 4
[INFO] 12.01.2021 17:36:04.110 [Timer-0] com.myproject.client.ui.wz.SmsCodeStepWz$2$1.run(SmsCodeStepWz.java:202) 
    leftSeconds = 3

But on Web page replace button's caption to 9. Nice.

But after one second NOT REPLACE by 8. And nothing happened. Button's caption is only 9.

As you can see I try to update button's caption on UI thread but it not help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I just tried your code with Vaadin version 8.12.0 and it works fine. It sounds like you don't have Server Push properly configured. In the simplest case, you need to add @Push annotation on your class for your code to work.

EDIT: The following similar example works fine in Vaadin 7.7.17.

@Push
public class MyUI extends UI {

    @Override
    protected void init(VaadinRequest vaadinRequest) {
        final VerticalLayout layout = new VerticalLayout();
        setContent(layout);

        Button button = new Button("", e -> Notification.show("Hello world"));
        layout.addComponents(button);

        button.setEnabled(false);
        final Timer timer = new Timer();

        timer.scheduleAtFixedRate(new TimerTask() {
            int leftSeconds = 10;

            @Override
            public void run() {
                leftSeconds--;

                UI.getCurrent().access(() -> button.setCaption(leftSeconds + ""));

                if (leftSeconds < 1) {
                    timer.cancel();
                    UI.getCurrent().access(() -> {
                        button.setCaption("Click me");
                        button.setEnabled(true);
                    });
                }
            }
        }, 0, 1000);

    }

    @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
    @VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
    public static class MyUIServlet extends VaadinServlet {
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...