OptionGroup with multiselect and valuechangelistener

Hello all,

I’d like to create an OptionGroup with multiselect set to true in which I have, for example, 4 options to choose. The first three options are Apple, Orange and Banana. The last option is All. In this case, I’d like to add a valuechangelistener to this optiongroup so that when I select All, it’ll clear the rest of the field since All includes all three types of items here. Nothing needs to be done when All is unselected.

How do I implement this?

Thank you.

Use OptionGroup.setMultSelect() to enable the Multi-Select-Mode.
And use OptionGroup.addValueChangeListener (new Property.ValuechangeListener(){…}) to add a ValueChangeListener.
in this Listener you can use event.getProperty().getValue() to get the currently clicked item. Then you can use OptionGroup.deselect(itemID) to deselect an item.

EDIT: in the listener you have to mind a little issue mentioned
here

I was obliged to change the code like that (vaadin 7.1.5)


        options.addValueChangeListener(new ValueChangeListener() {
			
			@Override
			public void valueChange(com.vaadin.data.Property.ValueChangeEvent event) {
				// TODO Auto-generated method stub
				Notification.show("xxx");
				
			}
		});

But my purpose is to get an event only when i click on an option, not at the change.

I had the same issue where I only wanted the listener to fire on click, not when the value changed (e.g. changing the item data source should not fire the listener). I think I have solved it by adding/ removing the listener inside a focus lisener and blur listener respectively like so:

[code]
final ValueChangeListener optionGroupValueChangeListener = new Property.ValueChangeListener() {
public void valueChange(ValueChangeEvent event) {
//Do whatever you want to do on click here
}
};

    optionGroup.addFocusListener(new FocusListener() {
        public void focus(final FocusEvent event) {
            optionGroup.addValueChangeListener(optionGroupValueChangeListener);
        }
    });
    
    optionGroup.addBlurListener(new BlurListener() {
        @Override
        public void blur(final BlurEvent event) {
            optionGroup.removeValueChangeListener(optionGroupValueChangeListener);
        }
    });

[/code]The value change listener is thefore added when the optiongroup gets the focus, and is removed when it loses the focus so only fires when clicked!