Reputation: 47
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
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
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