ZK Radiogroup返回了错误的getSelectedIndex()

时间:2019-01-16 08:50:58

标签: java zk

我正在使用ZK 8.5.2.1,并且具有带有Radiogroup的弹出窗口。 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>

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
}

因此,当屏幕上显示弹出窗口并且我选择了一些单选项目并按“接受”按钮时-selectedIndex并不总是正确的。有时为-1,有时为旧选择值,有时为正确值。怎么了?

1 个答案:

答案 0 :(得分:1)

我简化了您的代码,并且它始终获得正确的选定索引。

    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);
        }
    }

您需要在致电getSelectedIndex()之前跟踪发生的情况。您可以在onClick侦听器中注释掉代码,然后逐步逐行添加代码以找出根本原因。