JeffStrongman
JeffStrongman

Reputation: 47

Vaadin refresh grid after adding row

My grid is not refreshing automatically after adding a row. I've tried several solutions from other questions but they didn't work. E.g. grid.clearSortOrder(); and grid.markAsDirty();. My goal is to add rows after time periods. Therefore I'm using a Timer and the rows are added but the grid does not refresh until I click in the table.

Easy code example:

Grid grid = new Grid();

grid.addColumn("Name");
grid.addColumn("Age");

Timer timer = new Timer();
timer.schedule(new TimerTask(){
    public void run(){
        grid.addRow("Exmaple","99");    
    }
}, 1000, 1000);

Upvotes: 0

Views: 922

Answers (2)

Raffaele
Raffaele

Reputation: 20875

Vaadin callbacks are triggered by user interactions like mouse press on a button, selection of an option and so on. When you need the user interface to reflect a change that was not caused by the mouse/keyboard, you need to enable server push:

@Push
public class App extends UI {

  @Override
  public void init(VaadinRequest request) {
    timer.schedule(new TimerTask() {
      public void run() {
        access(() -> grid.addRow("Example", "99"));
      }
    }, 1000, 1000);
  }
}

Note the usage of UI.access(Runnable) to lock the UI when it is accessed from a non-request thread.

Upvotes: 2

Thomas Philipp
Thomas Philipp

Reputation: 276

You will Need Server push for this. See Vaadin-Doc-Serverpush. You want to change the UI from another Thread (Timer.schedule() will execute in another thread).

Upvotes: 3

Related Questions