Just a guess, but maybe the CheckBoxes are in “non-immediate” mode. You can verify it by adding a button next to your table, which will push all pending changes to the server when clicked (you only need to add a normal button, no listeners needed). If the values get updated after clicking the button, the checkboxes are non-immediate.
By default checkboxes are non-immediate, and I’m not sure how the Table’s editable mode generates the CheckBoxes. To make a checkbox immediate (or any other component) just call “setImmediate(true)” on it.
This is an interesting issue. I tried it and the getter is called but the setter is not. I didn’t use a custom container though, just the built-in container of Table.
This is probably a bug. I’ll try to look into it a bit further. The main developers are now on vacation, so I can’t get a comment right now.
Below is my test code:
package com.itmill.toolkit.tests.book;
import com.itmill.toolkit.data.Property;
import com.itmill.toolkit.data.Property.ValueChangeEvent;
import com.itmill.toolkit.data.util.BeanItem;
import com.itmill.toolkit.ui.BaseFieldFactory;
import com.itmill.toolkit.ui.CheckBox;
import com.itmill.toolkit.ui.Component;
import com.itmill.toolkit.ui.CustomComponent;
import com.itmill.toolkit.ui.Field;
import com.itmill.toolkit.ui.OrderedLayout;
import com.itmill.toolkit.ui.Table;
public class TableEditableBean extends CustomComponent {
public class MyBean {
boolean selected1;
boolean selected2;
public MyBean() {
selected1 = false;
selected2 = false;
}
public boolean isSelected1() {
return selected1;
}
public void setSelected1(boolean selected) {
this.selected1 = selected;
System.out.println("setSelected1("+selected1+") called.");
}
public boolean isSelected2() {
return selected2;
}
public void setSelected2(boolean selected) {
this.selected2 = selected;
System.out.println("setSelected2("+selected+") called.");
}
};
public class MyFieldFactory extends BaseFieldFactory {
public Field createField(Class type, Component uiContext) {
// Boolean field
if (Boolean.class.isAssignableFrom(type)) {
final CheckBox checkbox = new CheckBox();
checkbox.setSwitchMode(true);
checkbox.setImmediate(true);
return checkbox;
}
return super.createField(type, uiContext);
}
}
TableEditableBean() {
/* A layout needed for the example. */
OrderedLayout layout = new OrderedLayout(OrderedLayout.ORIENTATION_VERTICAL);
setCompositionRoot(layout);
// Create a table. It is by default not editable.
final Table table = new Table();
layout.addComponent(table);
// Define the names and data types of columns.
table.addContainerProperty("selected1", Boolean.class, null);
table.addContainerProperty("selected2", Boolean.class, null);
// Add a few items in the table.
for (int i=0; i<100; i++) {
MyBean item = new MyBean();
BeanItem bitem = new BeanItem(item);
table.addItem(bitem);
//table.addItem(new Object[] {bitem,bitem},
// new Integer(i)); // Item identifier
}
//table.setWriteThrough(true);
//table.setReadThrough(true);
// Use custom field factory that sets the checkboxes in immediate mode.
table.setFieldFactory(new MyFieldFactory());
final CheckBox switchEditable = new CheckBox("Editable");
switchEditable.addListener(new Property.ValueChangeListener() {
public void valueChange(ValueChangeEvent event) {
//table.commit();
table.setEditable(((Boolean)event.getProperty().getValue()).booleanValue());
}
});
switchEditable.setImmediate(true);
layout.addComponent(switchEditable);
}
}
One way is to create them with a custom field factory, as I did in the example above. But I don’t think the immediateness is the issue here, so setting them as immediate does not help (tried it). The default BaseFieldFactory explicitly sets the fields as non-immediate, which should be ok usually. Strangely, BaseFieldFactory uses Button with setSwitchMode(true) for boolean values and not CheckBox. Not that it should matter.
Did you use IndexedContainer (the default container in Table) or a custom container? I think the problem occurs only with IndexedContainer, because it doesn’t seem to bind the properties to the actual item properties. I could be wrong.
Ok, I tried the Table with a simple custom container for bean items. The problem does not occur there and the table works as you would expect. Both the setters and getters of the bean are called properly.
package com.itmill.toolkit.tests.book;
import java.util.Collection;
import java.util.Vector;
import com.itmill.toolkit.data.Container;
import com.itmill.toolkit.data.Item;
import com.itmill.toolkit.data.Property;
import com.itmill.toolkit.data.Property.ValueChangeEvent;
import com.itmill.toolkit.data.util.BeanItem;
import com.itmill.toolkit.ui.AbstractField;
import com.itmill.toolkit.ui.BaseFieldFactory;
import com.itmill.toolkit.ui.CheckBox;
import com.itmill.toolkit.ui.Component;
import com.itmill.toolkit.ui.CustomComponent;
import com.itmill.toolkit.ui.Field;
import com.itmill.toolkit.ui.OrderedLayout;
import com.itmill.toolkit.ui.Table;
/**
* Shows how to bind a bean to a table and make it editable.
*/
public class TableEditableBean extends CustomComponent {
/**
* Let's have a simple example bean.
*/
public class MyBean {
boolean selected;
String text;
public MyBean() {
this.selected = false;
this.text = "";
}
public boolean isSelected() {
System.out.println("isSelected() called: " + selected);
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
System.out.println("setSelected1("+selected+") called.");
}
public String getText() {
System.out.println("getText() called: " + text);
return text;
}
public void setText(String text) {
this.text = text;
System.out.println("setText("+text+") called.");
}
};
/**
* Custom field factory that sets the fields as immediate for debugging
* purposes. This is not normally necessary, unless you want to have some
* interaction that requires it.
*/
public class MyFieldFactory extends BaseFieldFactory {
public Field createField(Class type, Component uiContext) {
// Let the BaseFieldFactory create the fields
Field field = super.createField(type, uiContext);
// ...and just set them as immediate
((AbstractField)field).setImmediate(true);
return field;
}
}
/**
* This is a custom container that allows adding BeanItems inside it. The
* BeanItem objects must be bound to a MyBean object. The item ID is an
* Integer from 0 to 99.
*
* Most of the interface methods are implemented with just dummy
* implementations, as they are not needed in this example.
*/
public class MyContainer implements Container {
Item[] items;
int current = 0;
public MyContainer() {
items = new Item[100]
; // Yeah this is just a test
}
public boolean addContainerProperty(Object propertyId, Class type,
Object defaultValue) throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
public Item addItem(Object itemId) throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
public Object addItem() throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
/**
* This addItem method is specific for this container and allows adding
* BeanItem objects. The BeanItems must be bound to MyBean objects.
*/
public void addItem(BeanItem item) throws UnsupportedOperationException {
items[current++]
= item;
}
public boolean containsId(Object itemId) {
if (itemId instanceof Integer) {
int pos = ((Integer)itemId).intValue();
if (pos >= 0 && pos < 100)
return items[pos]
!= null;
}
return false;
}
/**
* The Table will call this method to get the property objects for the
* columns. It uses the property objects to determine the data types of
* the columns.
*/
public Property getContainerProperty(Object itemId, Object propertyId) {
if (itemId instanceof Integer) {
int pos = ((Integer)itemId).intValue();
if (pos >= 0 && pos < 100) {
Item item = items[pos]
;
// The BeanItem provides the property objects for the items.
return item.getItemProperty(propertyId);
}
}
return null;
}
/** Table calls this to get the column names. */
public Collection getContainerPropertyIds() {
// This container can contain only BeanItems bound to MyBeans.
Item item = new BeanItem(new MyBean());
// The BeanItem knows how to get the property names from the bean.
return item.getItemPropertyIds();
}
public Item getItem(Object itemId) {
if (itemId instanceof Integer) {
int pos = ((Integer)itemId).intValue();
if (pos >= 0 && pos < 100)
return items[pos]
;
}
return null;
}
public Collection getItemIds() {
Vector ids = new Vector();
for (int i=0; i<100; i++)
ids.add(Integer.valueOf(i));
return ids;
}
public Class getType(Object propertyId) {
return BeanItem.class;
}
public boolean removeAllItems() throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
public boolean removeContainerProperty(Object propertyId)
throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
public boolean removeItem(Object itemId)
throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
public int size() {
return current;
}
}
TableEditableBean() {
/* A layout needed for the example. */
OrderedLayout layout = new OrderedLayout(OrderedLayout.ORIENTATION_VERTICAL);
setCompositionRoot(layout);
// Create a table. It is by default not editable.
final Table table = new Table();
layout.addComponent(table);
table.setPageLength(8);
// Use the custom container as the data source
MyContainer myContainer = new MyContainer();
table.setContainerDataSource(myContainer);
// Add a few items in the table.
for (int i=0; i<5; i++) {
// Create the bean
MyBean item = new MyBean();
item.setText("MyBean " + i);
// Have an Item that is bound to the bean
BeanItem bitem = new BeanItem(item);
// Add the item directly to the container using the custom addItem()
// method. We could otherwise add it to the Table as well, but
// the Container interface of Table does not allow adding items
// as such, just item IDs.
myContainer.addItem(bitem);
}
// Use custom field factory that sets the checkboxes in immediate mode.
// This is just for debugging purposes and is not normally necessary.
table.setFieldFactory(new MyFieldFactory());
// Have a check box to switch the table between normal and editable mode.
final CheckBox switchEditable = new CheckBox("Editable");
switchEditable.addListener(new Property.ValueChangeListener() {
public void valueChange(ValueChangeEvent event) {
table.setEditable(((Boolean)event.getProperty().getValue()).booleanValue());
}
});
switchEditable.setImmediate(true);
layout.addComponent(switchEditable);
}
}