CheckBox inside table, with opportunity to check/uncheck all records

Hello. I my project i need to add table, with checkbox inside, and that person can check/uncheck all records. I find one work around for this problem, may be some one find it helpful for they project. Here I put only part on my code.

private BeanItemContainer<VaadinBasicBean> beanItemContainer;
public class VaadinBasicBean implements Serializable {
	//some variables
	private boolean selected = false;
	}
private Table vaadinBasicTable() {
		final Table table = new Table();
		table.setImmediate(true);
		table.setSizeFull();

		beanItemContainer = new BeanItemContainer<VaadinBasicBean>(VaadinBasicBean.class);

		beanItemContainer.addAll(// add some data);

		table.addGeneratedColumn("", new ColumnGenerator() {

			@Override
			public Component generateCell(final Table source, final Object itemId, final Object columnId) {

				final VaadinBasicBean bean = (VaadinBasicBean) itemId;

				final CheckBox checkBox = new CheckBox();
				checkBox.setImmediate(true);
				checkBox.addListener(new Property.ValueChangeListener() {
					@Override
					public void valueChange(final ValueChangeEvent event) {
						bean.setSelected((Boolean) event.getProperty().getValue());
					}
				});

				if (bean.isSelected()) {
					checkBox.setValue(true);
				} else {
					checkBox.setValue(false);
				}
				return checkBox;
			}
		});

		table.setContainerDataSource(beanItemContainer);

		// Set visible
		table.setVisibleColumns(//set visible columns);
		table.setImmediate(true);
		table.setSelectable(true);

		return table;
	}

This checkBox can be added inside any component.

checkBox.addListener(new Property.ValueChangeListener() {

			@Override
			public void valueChange(final ValueChangeEvent event) {
				if ((Boolean) event.getProperty().getValue()) {
					toggleBeanItemContainer(table, true);
				} else {
					toggleBeanItemContainer(table, false);
				}
			}
		});
private void toggleBeanItemContainer(final Table table, final boolean select) {
	Collection<VaadinBasicBean> temp = new LinkedList<VaadinBasicBean>();

		for (VaadinBasicBean bean : beanItemContainer.getItemIds()) {
			bean.setSelected(select);
			temp.add(bean);
		}
		beanItemContainer.addAll(beanItemContainer.getItemIds());

		Object[] properties = { "" };
		boolean[] ordering = { true };

		table.sort(properties, ordering);
	}

And finally we get this. Such method, I think is very useful, if you need to make one operation with few objects. In my project I added filter panel, that allow users put some filter criteria for table data and after make with them some actions.
11576.png

I guess you could put all the checkBoxes to a Map and change boolean values of CheckBoxes from there.

Just remember that the generated column is generated (and regenerated) dynamically as the user scrolls the table, so only those CheckBoxes exist that are currently visible (or have previously been if you store those). This makes the required handling a bit complex.

But checkbox will be checked/unchecked depends on value in BeanItemContainer and when user will scroll table, this dynamic column will be generated with right state.