Search code examples
javazk

ZK Radiogroup returns incorrect getSelectedIndex()


I am using ZK 8.5.2.1 and I have popup window with Radiogroup. The zul:

<zk xmlns="http://www.zkoss.org/2005/zul" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul/zul.xsd">
    <popup use="com.myproject.webapp.docbrowse.filter.FiltrationModePopup" width="330px">
            <vlayout sclass="content">
                <hlayout>
                    <label sclass="title" value="List form setup"/>
                </hlayout>
                <radiogroup id="rgScrollerMode" sclass="scrollermode-radio">
                    <grid>
                        <rows>
                            <row sclass="radio-border">
                                <radio label="Standart"/>
                            </row>
                            <row sclass="radio-border">
                                <radio label="Archive / Select year"/>
                                <hlayout id="archivePanel" use="com.myproject.webapp.docbrowse.ArchivePanel"/>
                            </row>
                            <row sclass="radio-border">
                                <radio label="Trash"/>
                            </row>
                        </rows>
                    </grid>
                </radiogroup>
        . . .
                <div align="right">
                    <button id="okButton" label="Accept" sclass="acceptButton"/>
                    <button id="cancelButton" label="Cancel" sclass="cancelButton"/>
                </div>
            </vlayout>
    </popup>
</zk>

Class FiltrationModePopup:

public class FiltrationModePopup extends Popup implements AfterCompose, IdSpace {
    public static final String STANDARD_MODE = "/icons/toolbar/table_mode.png";
    private static final String ARCHIVE_MODE = "/icons/toolbar/archive_mode.png";
    private static final String RECYCLEBIN_MODE = "/icons/toolbar/recycle_mode.png";
    private Radiogroup rgScrollerMode;
    //. . . other properties

    @Override
    public void afterCompose() {
        . . .
        rgScrollerMode = (Radiogroup) getFellow("rgScrollerMode");
        final Button okButton = (Button) getFellow("okButton");
        okButton.addEventListener(Events.ON_CLICK, new SerializableEventListener<Event>() {
            @Override
            public void onEvent(Event event) {
                filtrationModeSaver.save(FiltrationModePopup.this);
                final FiltrationSettings filtrationSettings = createFiltrationSettingsFromPopup();
                EventUtils.postEvent(new FiltersChangeEvent(FiltrationModePopup.this, filtrationSettings));
                EventUtils.postEvent(new RefreshScrollerEvent(FiltrationModePopup.this));

                close();
            }
        });
        addForward(Events.ON_OK, okButton, Events.ON_CLICK);
        EventUtils.registerHandler(this);
    }

    private FiltrationSettings createFiltrationSettingsFromPopup() {
        return new FiltrationSettings(getScrollerMode(), getArchiveValue());
    }

    public ScrollerMode getScrollerMode() {
        switch (rgScrollerMode.getSelectedIndex()) {
            case 1:
                return ScrollerMode.ARCHIVE;
            case 2:
                return ScrollerMode.RECYCLEBIN;
            default:
                return ScrollerMode.STANDARD;
        }
    }
    // . . . Other code
}

So, when popup displays on screen and I selecting some Radio item and pressing "Accept" button - selectedIndex is not always correct. Sometimes it's -1, sometimes it's old selected value and sometimes it's correct. What's wrong?


Solution

  • I simplify your code, and it always get the correct selected index.

        public class FiltrationModePopup extends Popup implements AfterCompose, IdSpace {
            private Radiogroup rgScrollerMode;
    
            @Override
            public void afterCompose() {
                rgScrollerMode = (Radiogroup) getFellow("rgScrollerMode");
                final Button okButton = (Button) getFellow("okButton");
                okButton.addEventListener(Events.ON_CLICK, new SerializableEventListener<Event>() {
                    @Override
                    public void onEvent(Event event) {
                        System.out.println(rgScrollerMode.getSelectedIndex());
                        close();
                    }
                });
                addForward(Events.ON_OK, okButton, Events.ON_CLICK);
            }
        }
    

    You need to trace what happens before you call getSelectedIndex(). You can comment out codes inside onClick listener and gradually add codes line by line to find out the root cause.