Table - sorting by a column containing CheckBox-s

Hi, nice to meet you guys

I have the following case and I am unable to find an easy solution to it so far:

One of my table’s columns is supposed to show checkboxes, as in

table.addContainerProperty(ITEM_PROPERTY_ID_SUBSCRIBED, [b]
Component.class
[/b], null);

On the other hand, I need to be able to sort by this column.
As Boolean implements Comparable this could work fine, but then again I need to visualize the Boolean values as CheckBox components. And as far as I know, both sorting and visualizing use the same Property type, which is fine but it’s causing me trouble.

I would appreciate any suggestions and comments.

Regards,
Vassil

You could extend CheckBox and implement Comparable.

You could also use a custom ItemSorter or a DefaultItemSorter with a custom Comparator.

The alternative for using components as properties is to use the actual data type (Boolean) as property, and then put the table in editable mode, and the values will be editable as CheckBoxes.

If you are a Pro Account subscriber, see article
#180
.

If I remember correctly, one more option in addition to the ones mentioned by Marko could be having an underlying Boolean column and “overriding” it on the UI with a generated column with the same property name.

Thanks a lot :slight_smile: I will consider both the generated columns and the Comparable CheckBox-s

There are 2 other approaches:

  1. Fix the Vaadin CheckBox to be Compariable

  2. Create a simple subclass that uses comparaiable, as show in the following

[public class CheckBoxComparable extends CheckBox implements Comparable {

public CheckBoxComparable() {
    super();
}

public CheckBoxComparable(String caption) {
    super(caption);
}

public CheckBoxComparable(String caption, boolean initialState) {
    super(caption, initialState);
}

public CheckBoxComparable(String caption, Property<?> dataSource) {
    super(caption, dataSource);
}

@Override
public int compareTo(CheckBox o) {
    return Boolean.compare(this.booleanValue(), o.booleanValue());
}

}