Can I create singlenton vaadin combobox component?

Viewed 52

On a Vaadin application, I instanciate a same combobox on each tab.

These combobox contains a large information list. That takes huge memory on the server.

I have three tabs, therefore, the combobox appears three times.

I wish create one combobox on my application with a pattern singleton.

And this one combobox will be shared by three tabs.

But if I apply this pattern singleton, the combobox appears at once on only one tab.

How can create a singleton combobox and this last appears on each tab ?

Thanks by advance.

1 Answers

Vaadin component can be attached to one UI only, and one layout at the time. Thus Vaadin component cannot be singleton.

However, you have number other options to solve your actual problem.

The first and the most obvious thing is that you can use data provider from callbacks in your ComboBox. If the information list is large, this is the first thing you should do. When you use data provider from callbacks, only small buffer of items is kept in memory and as user scrolls the ComboBox.

DataProvider<Bean, Void> dataProvider =
    DataProvider.fromCallbacks(
        query -> {
            List<Bean> beans = getBeansService()
                    .fetchBeans(query.getOffset(), query.getLimit());
            return beans.stream();
        },
        query -> getBeansService().getBeansCount());
);

ComboBox<Bean> combo = new ComboBox<>();
combo.setDataProvider(dataProvider);

When you setup the ComboBox like this, you can easily have multiple instance of the ComboBox.

In case you still use in memory data provider, you can actually share it with multiple component instances.

ListDataProvider<Bean> dataProvider = DataProvider.ofCollection(items);
comboBox1.setDataProvider(dataProvider);
comboBox2.setDataProvider(dataProvider);
Related