Uncheck a checkbox if another checkbox is checked

How to Uncheck a checkbox if another is checked …

You can unselect another CheckBox by adding a ValueChangeListener to the first one and from within changing the value, but watch out for infinite event loops. It is not a good idea having check boxes unselecting each other. A better idea might be to use an OptionGroup where only one value can be selected once.

Thanks Johan,
I wrote some code in addValueChangeListener but it will working as…
1.When i was select a checkbox…data is loading
2. aftre data loading previous checkbox is uncheking…

but what i want is when i select a checkbox…immidiatly previous checkbox is uncheked without loading data.


private void addChildrenToTreeTable(Object objId, int intPIndex) {

    if (trMachines.getChildren(objId) != null) {
        for (Object objCId : trMachines.getChildren(objId)) {
            String strCaption = trMachines.getItemCaption(objCId);
                final CheckBox cb = new CheckBox(strCaption.substring(0, strCaption.indexOf("_")));
                cb.addValueChangeListener(new ValueChangeListener() {
                private static final long serialVersionUID = 5605240972867331012L;
                @SuppressWarnings({ "unchecked" })
                public void valueChange(ValueChangeEvent event) {
                    if (cb.getValue()) {
                        for (Object objIdT : ttMachine.getItemIds()) {
                            CheckBox cbT = (CheckBox) ttMachine.getItem(objIdT).getItemProperty("Select Machine").getValue();
                            if (!cbT.isReadOnly() && !cbT.equals(cb)) {
                                ttMachine.getItem(objIdT).getItemProperty("Select Machine").setValue(cbT);

Based on !cbT.equals(cb), I make the assumption that ttMachine may contain also cb. So, when the valuechange event is fired for cb, you try to prevent itself from being set to null. However, that won’t work because as I assume all other checkBoxes also have a valueChangeListener. This means that for every check box that value changed for will also get their listeners called.

A workaround for you in this case could be to remove the value change listeners when setting the value and adding back.
You could add the valueChangeListener to for example checkbox.setData() of every check box and then inside the valuechange method (disclaimer: it is ugly):

cbT.removeValueChangeListener((ValueChangeListener) cbT.getData());
cbT.setValue(false); // Now this won't fire any further events
cbx.addValueChangeListener((ValueChangeListener) cbT.getData());

A nicer way would be to override setting the internal value for this purpose, by creating your own component:

    public class MyCheckBox extends CheckBox {

        public MyCheckBox(String caption) {

        public void setValueWithoutFiringValueChange(Boolean value) {

        protected void setInternalValue(Boolean newValue) {

and then setting the values in your valueChange method: