Update text label

Hello,

I have a label with a text. The text should be updated by a Background-Thread. I have already tried to call the function markAsDirty (), but the text is not updated. Is there a possibility to force to draw a Component ?

Are you using UI.access to update the label from the background thread?
Is server push enabled (for example with @Push annotation on UI class)?

Hello Marco,

yes i’m using a Push annotaition on UI class and i’m using UI.access .

This is my LoginWindow class which initialize my Background Thread.
And should update a Label

public class LoginWindow extends VerticalLayout{

private Label label;
private UpdateLabelClass updateLabel;

public LoginWindow() {
    setSizeFull();

    Component loginForm = buildLoginForm();
    addComponent(loginForm);
    setComponentAlignment(loginForm, Alignment.MIDDLE_CENTER);

}

private Component buildLoginForm() {
loginPanel = new VerticalLayout();
loginPanel.setSizeUndefined();
loginPanel.setSpacing(true);
Responsive.makeResponsive(loginPanel);
loginPanel.addStyleName(“login-panel”);
loginPanel.addComponent(buildFields());
loginPanel.addComponent(loadDataLabel());

    updateLabel= new UpdateLabelClass();
    updateLabel.setNumberOfSteps(7);
    updateLabel.setLabel(this.label);
    
    
    UI ui = UI.getCurrent();
    MyUI dashboardUI = (MyUI) ui;    
    
    dashboardUI.setUpdateLabel(updateLabel);
    
    return loginPanel;
}

private Component buildFields() {
HorizontalLayout fields = new HorizontalLayout();
fields.setSpacing(true);
fields.addStyleName(“fields”);

   Button signin = new Button("Sign In");
    signin.addStyleName(ValoTheme.BUTTON_PRIMARY);
    signin.setClickShortcut(KeyCode.ENTER);
    signin.focus();

    signin.addClickListener(new ClickListener() {
        @Override
        public void buttonClick(final ClickEvent event) {

                    label.setVisible(true);
                    new InitializerThread().start();
                    UI ui = UI.getCurrent();
                    MylUI dashboardUI=     (MylUII) ui;              
                    dashboardUI.setContent(new LoginWindow());
                    dashboardUI.addStyleName("loginview"); 
        }
    });

    fields.addComponent( signin);
    fields.setComponentAlignment(signin, Alignment.BOTTOM_LEFT);
    
    return fields;
}
public class InitializerThread extends Thread {
  
    public InitializerThread()
    {
    }
    @SuppressWarnings("static-access")
    @Override
    public void run() {
        UI ui = UI.getCurrent();
        MyUI dashboardUI = (MyUI) ui;
        
        dashboardUI.access(new Runnable() {
            @Override
            public void run() {
        
                dashboardUI.setContent(new MainView());
                dashboardUI.removeStyleName("loginview");
                
                
                
            }
        });
    }
}
public Component loadDataLabel() {

    label = new Label();
    label.setValue("0 of 7 loaded");
    label.addStyleName("labels");
    label.addStyleName(ValoTheme.LABEL_H4);
    label.addStyleName(ValoTheme.LABEL_COLORED);
    label.setVisible(false);

    return label;

}

}// End of LoginWindowClass

//Start pdateLabel Class
public class UpdateLabelClass implements Serializable {

int numberOfSteps = 0;
Component label;

public UpdateLabelClass () {

}

public void setLabel(Component label)
{
this.label = label;
}

public void setNumberOfSteps(int steps)
{
numberOfSteps = steps;
}

public void progress(int step) {
String caption = “Step” + step + " of" + this.numberOfSteps + " Steps";

this.label.setCaption(caption);
this.label.markAsDirty();

}
} // end of UpdateLabelClass

// start MyUI class
@Push
public class MyUI extends UI implements Serializable {

private UpdateLabelClass updateLabel;

public void setUpdateLabel (UpdateLabelClass updateLabel) {
    this.updateLabel= updateLabel;
}

public UpdateLabelClass getUpdateLabelClass () {
    return this.updateLabel;
}

@WebServlet(value = "/*", asyncSupported = true)
public static class Servlet extends VaadinServlet {
}

@Override
protected void init(VaadinRequest request) {


    setLocale(Locale.GERMANY);
    Responsive.makeResponsive(this);
    addStyleName(ValoTheme.UI_WITH_MENU);
    setContent(new LoginWindow());
    addStyleName("loginview");

}

} // end of UI

I am in despair

I might be blind (a lot of rows here) but it seems like you never call the progress-method from anywhere. Where do you expect that method to be called?

i found the solution.
i put to my method progress : UI.getCurrent().push();

Thank you @Anna Becker for providing problem and solution. it helped me. Thanks a million.