在Vaadin应用程序中动态更新网格

时间:2017-03-14 11:18:21

标签: java spring vaadin

我有一个Vaadin应用程序,它使用Grid来显示来自后台线程的日志消息。当我将元素添加到日志消息列表时,有没有办法刷新网格?在下面的例子中,我使用按钮来模拟日志事件。

我的UI类

@SpringUI
@Push
public class MyUI extends UI {
    @Autowired
    private Logger logger;

    @Override
    protected void init(VaadinRequest vaadinRequest) {
        //container
        VerticalLayout content = new VerticalLayout();
        setContent(content);
        //grid
        Grid<LogEntry> grid = new Grid<>();
        grid.addColumn(LogEntry::getDateTime);
        grid.addColumn(LogEntry::getMessage);
        grid.setDataProvider(DataProvider.ofCollection(logger.getEntries()));
        content.addComponent(grid);
        //button to simulate log event
        Button button = new Button("Log"); 
        button.addClickListener(e -> logger.log(UUID.randomUUID().toString()));
        content.addComponent(button);
    }
}

我的记录器类

@SpringComponent
@VaadinSessionScope
public class Logger {
    private List<LogEntry> entries = new ArrayList<>();

    public List<LogEntry> getEntries() {
        return entries;
    }

    public void log(String message){
        LogEntry entry = new LogEntry(message);
        entries.add(entry);
    }
}

我的LogEntry类

public class LogEntry {
    private final LocalDateTime dateTime;
    private final String message;

    public LogEntry(String message) {
        this.dateTime = LocalDateTime.now();
        this.message = message;
    }

    public LocalDateTime getDateTime() {
        return dateTime;
    }

    public String getMessage() {
        return message;
    }
}

0 个答案:

没有答案