I just tried your code with Vaadin version 8.12.0 and it works fine. It sounds like you might not have Server Push properly configured. In the simplest case, you need to add @Push annotation on your class for your code to work.
Tarek Oraby:
I just tried your code with Vaadin version 8.12.0 and it works fine. It sounds like you might not have Server Push properly configured. In the simplest case, you need to add @Push annotation on your class for your code to work.
I try with @Push but it not help
import com.vaadin.annotations.Push;
@SuppressWarnings("serial")
@Push
public class SmsCodeStepWz extends RootStep {
...
}
@SuppressWarnings("serial")
public abstract class RootStep extends VerticalLayout {
I’m at loss with your code. It doesn’t show how you are adding the Buttons to your VerticalLayout, nor does it show the parent UI of the VerticalLayout.
If I would guess, you are missing @Push on the UI that adds SmsCodeStepWz as one of its child-components (which you don’t show us here).
Tarek Oraby:
I’m at loss with your code. It doesn’t show how you are adding the Buttons to your VerticalLayout, nor does it show the parent UI of the VerticalLayout.
If I would guess, you are missing @Push on the UI that adds SmsCodeStepWz as one of its child-components (which you don’t show us here).
here additional code:
@SuppressWarnings("serial")
public abstract class RootStep extends VerticalLayout {
public RootStep() {
postInitContent()
nextBlock = new NextRefresh(isRefreshRequired(), getNextActionTitle());
nextBlock.setNextListener(nextListener);
addComponent(nextBlock);
}
protected void postInitContent() {};
}
@SuppressWarnings("serial")
public class NextRefresh extends VerticalLayout {
public static String COMMON_HORIZONTAL_LEFT = "COMMON_HORIZONTAL_LEFT";
}
@SuppressWarnings("serial")
@Push
public class SmsCodeStepWz extends RootStep {
private Button buttonNext, buttonSendSMS;
private HorizontalLayout commonHorizontalLayout;
@Override
protected void initContent() {
buttonSendSMS = new Button(MessageService.getMessage("resend.sms"));
buttonSendSMS.addClickListener(new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
logger.debug("Click_send_sms_button");
MyUI myUI = new MyUI();
myUI.initTimer();
}
});
}
@Override
protected void postInitContent() {
Button nextButton = (Button) findById(this, NextRefresh.NEXT_BUTTON_ID);
if (nextButton != null) {
nextButton.setVisible(false);
}
commonHorizontalLayout = (HorizontalLayout) findById(this, NextRefresh.COMMON_HORIZONTAL_LEFT);
commonHorizontalLayout.addComponent(buttonSendSMS);
Label labelGap = new Label("");
labelGap.setWidth("10px");
commonHorizontalLayout.addComponent(labelGap);
}
@Push
public class MyUI extends UI {
public void initTimer() {
logger.info("initTimer:");
buttonSendSMS.setEnabled(false);
final Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
int leftSeconds = 10;
@Override
public void run() {
leftSeconds--;
UI.getCurrent().access(new Runnable() {
@Override
public void run() {
logger.info("leftSeconds = " + leftSeconds);
buttonSendSMS.setCaption(leftSeconds + "");
}
});
if (leftSeconds < 1) {
UI.getCurrent().access(new Runnable() {
@Override
public void run() {
timer.cancel();
logger.info("leftSeconds = " + leftSeconds);
buttonSendSMS.setEnabled(true);
new MessageService(getWzContext().getLang());
buttonSendSMS.setCaption(MessageService.getMessage("resend.sms"));
}
});
}
}
}, 0, 1000);
}
@Override
protected void init(VaadinRequest request) {
logger.info("init:");
}
}
The RootStep is abstract class.
And here how I add SmsCodeStepWz
public class UbUI extends UI {
@Override
protected void init(final VaadinRequest request) {
SmsCodeStepWz smsCodeStep = new SmsCodeStepWz();
smsCodeStep.setCallback(smsCallback);
addStep(smsCodeStep);
}
}
Was called before start my countdown. But when I call this method after finish my countdown then then button’s caption success updated 9,8,7 and so on. Nice.
The question is:Why update of button is depend on ?
UI.getCurrent().setPollInterval(-1);
Here result worked version:
private void runCountDown() {
new Thread(new Runnable() {
@Override
public void run() {
try {
while (true) {
countDownSec--;
UI.getCurrent().access(new Runnable() {
@Override
public void run() {
logger.info("countDownSec = " + countDownSec);
buttonSendSMS.setCaption(countDownSec + "");
}
});
Thread.sleep(1000);
if (countDownSec < 1) {
break;
}
} // end while
UI.getCurrent().setPollInterval(-1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}