Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

Maintaining CheckBox status

Aatif Hasan
9 years ago Jul 03, 2012 8:31am

Hi.. All,

I'll be thankful if someone could help me. Actually i'm using Vaadin Pagination(PagedTable) and BeanItemContainer. I am able to add a column of CheckBox and page navigation is also working like a charm. :)

But the problem is i'm unable to Maintain the Status of CheckBoxes. As i check any CheckBox and navigate to the next page the CheckBoxes are getting unchecked automatically while returning back to the previous page. I tried a lot to save the status of CheckBoxes in the been class by adding "isChecked" boolean property but still no luck.

Paged Table images with2 pages are attached below.!

Please anyone help me out or provide me any link.

Thanks in advance.!!
Aatif

Aatif Hasan
9 years ago Jul 03, 2012 10:33am

Yeah... I solved it. :)

Here is the following code snippet : -

Add boolean property In Bean Class-

1.) boolean isChecked = false;

public boolean isChecked() {
return isChecked;
}

public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
}
2.) Main Class-

// Create a container for the beans
final BeanItemContainer<BeanClass> beans = new BeanItemContainer<BeanClass>(
BeanClass.class);

// Use the name property as the item ID of the bean

// ArrayList<BeanClass> beanlist = new ArrayList<BeanClass>();
// Add some beans to it
beans.addBean(new BeanClass("Mung bean", 1452.0));
beans.addBean(new BeanClass("Chickpea", 686.0));
beans.addBean(new BeanClass("Lentil", 1477.0));
beans.addBean(new BeanClass("Common bean", 129.0));
beans.addBean(new BeanClass("Soybean", 1866.0));

/* This set contains the ids of the "selected" items */
// Bind a table to it
final PagedTable pgtable = new PagedTable("Beans of All Sorts");
pgtable.setContainerDataSource(beans);
pgtable.setVisibleColumns(new Object[] { "name", "energy" });
pgtable.setPageLength(3);

pgtable.addGeneratedColumn("selected", new Table.ColumnGenerator() {
@SuppressWarnings({ "serial", "serial" })
public Object generateCell(Table source, final Object itemId,
Object columnId) {

final BeanClass bean = (BeanClass) itemId;
// final BeanClass entity = (BeanClass) itemId;
// boolean selected = false;// =
// selectedItemIds.contains(itemId);
final CheckBox cb = new CheckBox();
cb.setImmediate(true);
cb.addListener(new Property.ValueChangeListener() {
public void valueChange(Property.ValueChangeEvent event) {

bean.setChecked(((Boolean) event.getProperty()
.getValue()));
}
});
if (bean.isChecked()) {
cb.setValue(true);
} else {
cb.setValue(false);
}

return cb;
}
});
vl.addComponent(pgtable);

vl.addComponent(new Button("<", new Button.ClickListener() {
public void buttonClick(Button.ClickEvent event) {
System.out.println("Run back..");
pgtable.previousPage();
}
}));

vl.addComponent(new Button(">", new Button.ClickListener() {
public void buttonClick(Button.ClickEvent event) {
System.out.println("Move ahead..");
pgtable.nextPage();
}
}));

Cheers.!!
Aatif

Last updated on Jul, 3rd 2012