ZK Radiogroup returns 不正确的 getSelectedIndex()
ZK Radiogroup returns incorrect getSelectedIndex()
我正在使用 ZK 8.5.2.1 并且我有 window 和 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>
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
}
因此,当弹出窗口显示在屏幕上并且我选择了一些 Radio 项目并按下 "Accept" 按钮时 - selectedIndex 并不总是正确的。有时它是-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监听里面的代码注释掉,逐行逐行添加代码,找出根本原因。
我正在使用 ZK 8.5.2.1 并且我有 window 和 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>
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
}
因此,当弹出窗口显示在屏幕上并且我选择了一些 Radio 项目并按下 "Accept" 按钮时 - selectedIndex 并不总是正确的。有时它是-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监听里面的代码注释掉,逐行逐行添加代码,找出根本原因。