R
R
Romario212019-02-21 12:48:36
Java
Romario21, 2019-02-21 12:48:36

Java framework Vaadin crookedly updated ui from child thread?

Hello everyone, I'll forgive the help of those who are familiar with the Vaadin framework.
In general, the essence of the problem:
The code block runs in a child thread, when the thread ends, you need to update the value of the ui component.
The value of the component changes, but the ui widget is not redrawn at the moment. And in order for it to be redrawn, you need to poke on any element that implements the listener, in general, it doesn’t matter what the main thing is that it has an addClickListener

@Push
public class WindowCreateLK  extends Composite<Div>{
  WindowCreateLK mainView;
  TextField tfDataFromServer;
  
    public WindowCreateLK(){
           this.mainView = this;
  
                //Этот виджет надо обновить из дочернего потока
           tfDataFromServer = new TextField("empty"); 
           
       
    
       Button someButton1 = new Button("Some button2");
       someButton1.addClickListener(e->{
       
       });
       Button someButton2 = new Button("Some button2");
       someButton2.addClickListener(e->{
       
       });
  
           getContent().add(tfDataFromServer,someButton1,someButton2);
       
       
       
           //My Thread
           MyCustomThread myThread = new MyCustomThread(UI.getCurrent(),context);
             myThread.start(); 
       
       
  }

    //inner class
    private static class MyCustomThread extends Thread {
        private final UI ui;
        private final WindowCreateLK mainView;

        private int count = 0;

        public MyCustomThread(UI ui, WindowCreateLK mainView) {
            this.ui = ui;
            this.mainView = mainView;
        }

        @Override
        public void run() {
            try {
                // Update the data for a while
                while (count < 10) {
                    Thread.sleep(500);
                    System.out.println(count);
                    count++;
                }

                // Inform that we are done
                ui.access(() -> {
                    ui.access(() -> mainView.tfDataFromServer.setValue("SomeData"));
                    ui.push();
          //But for view changes, i need click on some component with listener(like button or tab)

                });
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Carburn, 2019-11-21
@Carburn

.push()in ui.access()with it is @Pushnot necessary to call.

If the push mode is manual, you need to push the pending UI changes to the browser explicitly with the push() method.
https://vaadin.com/docs/v8/framework/advanced/adva...
push() should only be called with @Push(PushMode.MANUAL)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question