Single selected checkbox in Table

Hi,

I have addGeneratedColumn which contains CheckBox on my Table.

example:


public class EntityTable extends Table {

  public EntityTable {

    addGeneratedColumn("propertyName", new Table.ColumnGenerator() {
      public Component generateCell(Table source, Object itemId, Object columnId) {
        CheckBox cb = new CheckBox();   
        return cb;
      }
    });
}

Then how do I get a single selected checkbox, which means when I click on the checkbox, it’s selected, then when I click again to another checkbox, then the previous selected checkbox will unchecked.

Thanks.

You might want to see if a radio set works for. It is meant to solve such issue. But if you really want to use a checkbox then you need to probably do this manually in a value change listener or override setvalue. Not sure which one will work best have never done it.

How to do manually in a value change listener or override setvalue? Can you provide a snippet code for this example, so I get a picture to work on it.

Thanks.

I found the solution. Update the BeanItemContainer.
Example:


public class MyTable extends Table {
  
    public MyTable() {
        addGeneratedColumn("booleanProperty", new Table.ColumnGenerator() {
             public Component generateCell(Table source, Object itemId, Object columnId) {
                final Entity entity = (Entity) itemId;

                CheckBox cb = new CheckBox();   
                cb.setImmediate(true);
                cb.setValue(entity.getBooleanProperty());

                cb.addListener(new Button.ClickListener() {
                    public void buttonClick(ClickEvent event) {
                        entity.setBooleanProperty(event.getButton().booleanValue());
                        BeanItemContainer<Entity> container = (BeanItemContainer<Entity>) getContainerDataSource();
                        for (Iterator iterator = getItemIds().iterator(); iterator.hasNext();) {
                            
                            Entity bean = (Entity) iterator.next();
                            container.removeItem(bean);
                            if (!entity.equals(bean)) {
                                entity.setBooleanProperty(false);
                            }
                            container.addBean(bean);
                        }
                    }
                });
                return cb;
            }
        });
    }
}

Thanks.

You don’t need to remove and re-add the items, you could simply do container.getContainerProperty(itemId, propertyId).setValue(boolean) in the loop.

Hi,

Thank for reply.

I try to refactor code following your suggestion:


...
        addGeneratedColumn("booleanProperty", new Table.ColumnGenerator() {
             public Component generateCell(Table source, Object itemId, Object columnId) {
                final Entity checkedEntity = (Entity) itemId;

...

                        for (Iterator iterator = getItemIds().iterator(); iterator.hasNext();) {                            
                            Entity item = (Entity) iterator.next();
                            if (!checkedEntity.equals(item)) {
                                container.getContainerProperty(item, "booleanProperty").setValue(false);
                            }
                        }

...

When checkbox clicked, the previous selected checkbox is still checked.

Hi,

After exploring then I can simply update using container.getContainerProperty(itemId, propertyId).setValue(value). I need to do some try/catch.

Thanks.

/Shamsul

reset the container to the table it will work

for (Iterator iterator = getItemIds().iterator(); iterator.hasNext():wink: {
Entity item = (Entity) iterator.next();
if (!checkedEntity.equals(item)) {
container.getContainerProperty(item, “booleanProperty”).setValue(false);
}
}
tableName.setContainerDataSource(container,visilbeColumnLst);