Search code examples
javavaadinvaadin8vaadin-grid

Vaadin can't add items to grid


I have a grid which I am trying to update automatically from incoming websocket data. I am trying to add the new messages to the grids data holder, but I am getting multiple errors in the stacktrace.

@Push
public class MyUI extends UI implements Broadcaster.BroadcastListener {

VerticalLayout vertmain;
List<Trade> trades;

@Override
protected void init(VaadinRequest request) {

    vertmain = new VerticalLayout();
    vertmain.addComponent(new Label("start"));

    trades = Arrays.asList(Trade.newTrade(1, "buy"), Trade.newTrade(2, "sell"));

    Grid<Trade> grid = new Grid<>();
    grid.setItems(trades);
    grid.addColumn(Trade::getSide);
    grid.addColumn(Trade::getSize);

    vertmain.addComponent(grid);


    //start weboscket stream in new thread
    access(() -> {
        try {
            WebsocketSetup.startStream();
        } catch (Exception e) { e.printStackTrace(); }
    });

    // Register to receive broadcasts
    Broadcaster.register(this);

    setContent(vertmain);
}

@Override
public void receiveBroadcast(final String message) {
    // Must lock the session to execute logic safely
    access(new Runnable() {
        @Override
        public void run() {

            trades.add(Trade.newTrade(100, message));

        }
    });
}

I am new to vaadin and might be overlooking something, but have been messing with the grid and just can't get it to work..

stacktrace

Apr 03, 2018 12:35:57 AM com.vaadin.server.DefaultErrorHandler doDefault
SEVERE: 
java.util.concurrent.ExecutionException: java.lang.UnsupportedOperationException
    at java.base/java.util.concurrent.FutureTask.report(FutureTask.java:122)
    at java.base/java.util.concurrent.FutureTask.get(FutureTask.java:191)
    at com.vaadin.server.VaadinSession$FutureAccess.get(VaadinSession.java:114)
    at com.vaadin.server.VaadinService.runPendingAccessTasks(VaadinService.java:2049)
    at com.vaadin.server.VaadinSession.unlock(VaadinSession.java:1019)
    at com.vaadin.server.VaadinService.ensureAccessQueuePurged(VaadinService.java:2011)
    at com.vaadin.server.VaadinService.accessSession(VaadinService.java:1977)
    at com.vaadin.server.VaadinSession.access(VaadinSession.java:1418)
    at com.vaadin.ui.UI.access(UI.java:1562)
    at my.vaadin.MyUI.receiveBroadcast(MyUI.java:101)
    at my.vaadin.utils.Broadcaster$1.run(Broadcaster.java:35)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.lang.UnsupportedOperationException
    at java.base/java.util.AbstractList.add(AbstractList.java:153)
    at my.vaadin.MyUI.addTrade(MyUI.java:120)
    at my.vaadin.MyUI.access$000(MyUI.java:23)
    at my.vaadin.MyUI$2.run(MyUI.java:105)
    at com.vaadin.ui.UI.accessSynchronously(UI.java:1501)
    at com.vaadin.ui.UI$3.run(UI.java:1565)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:514)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at com.vaadin.server.VaadinService.runPendingAccessTasks(VaadinService.java:2046)
    ... 10 more

Solution

  • Arrays.asList returns an immutable List, meaning it can not be modified. This is not related to Vaadin.

    Instead, try creating a list the classic way: trades = new LinkedList<>(); trades.add(...);

    After adding a new item, you should probably refresh the Grid: grid.getDataProvider().refreshAll();