OptionGroup unselect event

I need to track check/uncheck event in multiple select option group .

OptionGroup group = new OptionGroup(“My Selection”);
group.setMultiSelect(true);
group.setImmediate(true);
group.addItems(“One”, “Two”, “Three”);
group.select(“One”);

group.addValueChangeListener(new ValueChangeListener()
{
@Override
public void valueChange(ValueChangeEvent event) {
Object selected = event.getProperty().getValue();
}
}
);

If I uncheck “One”, the selected is an empty array, how can I catch that “One” is unchecked event?

Hi,

you’ll need to keep track of the previous selection in code. So in your example case something like this (haven’t tested it, so there might be some typos):

private Set previousSelection = new HashSet();
//...

OptionGroup group = new OptionGroup("My Selection");
group.setMultiSelect(true);
group.setImmediate(true);
String one = "One";
String two = "Two";
String three ="Three";
group.addItems(one, two, three);
group.select(one);
previusSelection.add(one);

group.addValueChangeListener(new ValueChangeListener()
         {
                @Override
                public void valueChange(ValueChangeEvent event) {
                    Set selected = (Set) event.getProperty().getValue();
                    Set unselected = new HashSet();
                    unselected.addAll(previousSelection);
                    unselected.removeAll(selected);
                    // do something with unselected values
                    previousSelection = selected;
                }
        }
);

-Olli