How to generate a read-only check box?

I have a table in which one column is a Boolean which I want to render as a Checkbox in read-only mode.

To do this I must use

table.setEditable( true );

otherwise it paints as “true” instead of a Checkbox.

But I want the user to see a table which is not in edit mode in which he pushes an EDIT button to be editable.

Is there a way to have Booleans paint as a checkboxes and be able to setEditable false before the user sees it?

The answer:

	table.setEditable( true );

so that Boolean renders as a checkbox

	setReadOnly( true );

so that the Checkbox shows greyed-out

Two options:

Put CheckBoxes directly to you Table instead of Booleans:

Table table = new Table("Some items");
table.addContainerProperty("Col 1", CheckBox.class, null);
CheckBox cb = new CheckBox();
cb.setValue(true);
cb.setReadOnly(true);
table.addItem(new Object[]{cb},1);

Another option is to use the
TableFieldFactory.html
. Something like:

TableFieldFactory factory = new TableFieldFactory() {
	public  Field 	createField(Container container, Object itemId, Object propertyId, Component uiContext)  {
		Field field = DefaultFieldFactory.get().createField(container, itemId, propertyId, uiContext);
		if (propertyId.equals("Col 2")){
			field.setEnabled(false);
		}
		return field;
	}
};
table.setTableFieldFactory(factory);
table.setEditable(true);

Thanks for the suggestions.

The first does not really work for me as I use setDataSource to apply data to the table. I would like to keep it so that the representation of the data is quite separate from the data itself. For me the data should be Objects such as Strings, Booleans, Integers etc. My view should then determine the presentation.

I tried your second suggestion, overriding the DefaultFieldFactory with a custom one, but found that this was only looked at during the initial construction of the table and so was not responsive to changes in edit / not-edit modes.
I could improve this some by adding table.requestRepaint(), but my other columns show up as ugly disabled editable TextFields. This ends up not looking very nice.

So then I thought I would just override

Table.createField(Container container, Object itemId, Object propertyId, Component uiContext)

and render Boolean as Checkbox. But this does not work either because of code in

 Table.paintContent(PaintTarget target) throws PaintException

thus

           [code]

if ((iscomponent[currentColumn]
|| iseditable)
&& Component.class.isInstance(cells[CELL_FIRSTCOL + currentColumn]
[i]
)) {
final Component c = (Component) cells[CELL_FIRSTCOL + currentColumn]
[i]
;
if (c == null) {
target.addText(“”);
} else {
c.paint(target);
}
} else {
target.addText((String) cells[CELL_FIRSTCOL + currentColumn]
[i]
);
}
[/code]

If I am understanding this correctly, it seems that Table is insisting that any Component that is not editable should be rendered as text, so I guess that Boolean comes out as true. There seems no opportunity to toggle Enabled/disabled as this is not considered in PaintContent.

I think that what I am really after is that Table should always call c.paint(target) whatever the edit mode. The actual Component could then decide how to render itself using its attribs ‘readOnly’ and/or ‘enabled’. The decision to use a String representation could be made by the Component if it could not come up with anything better??? Combinations of the two attributes could allow four presentations?

I see no quick and easy Override that could do this. I would have to change Table itself.

This seems to be getting complicated.

Are there any simpler suggestions??