ZK - 从 ItemRenderer 访问 GUI 元素
ZK - Accessing GUI elements from ItemRenderer
我在访问 ZK 项目中模型 class 之外的 GUI 元素时遇到问题。我有一个 ListItemRenderer,它在我单击列表框中的元素时呈现数据和相应模型的位置,但是当我再次访问文章 class 以及所有 GUI 元素时,我的实例永久为空我第一次创建 class 时连接(通过 getFellow ())。这甚至是正确的方法吗?或者,我可以向变量添加一个侦听器,以便在更改文本框时重新加载该变量吗?感谢您的每一个提示,我已经经历了几个令人沮丧的小时,希望代码片段足够了。
祖尔:
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" root="./winArticles"?>
<window id="winArticles" title="Looking for Evidence" width="1100px" height="880px"
border="normal" use="xxx.Articles" closable="true" sizable="false">
...
<listbox id="artikel_output" width="100%" height="" multiple="true"
checkmark="true" vflex="true" nonselectableTags="*">
<listhead>
<listheader label="Artikel"/>
</listhead>
</listbox>
...
<div vflex="1">
<textbox style="font-size:50px"
id="tb_article_abstract"
multiline="true"/>
</div>
...
</window>
型号:
public class Articles extends Window implements AfterCompose,
IGenericDomainValueSelectorActions, IUpdateModal, IGenericListActions {
private static Articles instance;
public Articles() { }
public static Articles getInstance() {
if (instance == null) {
instance = new Articles();
}
logger.debug("Instance getInstance " + instance.getId());
return instance;
}
@Override
public void afterCompose() {
instance = new Articles();
tb_article_abstract = (Textbox) getFellow("tb_article_abstract");
...}
正在初始化列表框渲染器:
public void fillListBoxWithArticles(final Listbox lb, List<Article> articles) {
if (articles.size() == 0) {
lb.appendChild((Component) articles.get(0));
}
lb.setItemRenderer(new ArticleItemRenderer());
lb.setCheckmark(true);
lb.setMultiple(true);
ListModelList<Article> lml_articles = new ListModelList<Article>(articles);
lml_articles.setMultiple(true);
lb.setModel(lml_articles);
lb.focus();
}
如果选择了列表框元素,在文本框中显示它的属性,但文本框始终为空...
public void setArticleToInfobox(Article selectedArticle, int selectedIndex) {
this.selectedArticle = selectedArticle;
// tb_article_abstract = null
tb_article_abstract.setValue(selectedArticle.getAbstract_full());
tb_article_abstract.invalidate();
}}
ItemRenderer 正在获取所选项目的数据(和索引):
public class ArticleItemRenderer implements ListitemRenderer<Article> {
@Override
public void render(Listitem item, Article data, int index) throws Exception {
item.appendChild(new Listcell(data.getArticletitle() + ", " );
item.addEventListener(Events.ON_CLICK, new EventListener() {
@Override
public void onEvent(Event event) throws Exception {
EvidenceBasedArticles.getInstance().setArticleToInfobox(data, item.getIndex());
}});}
你在这里犯了一些严重的错误。
public static Articles getInstance() {
if (instance == null) {
instance = new Articles();
}
logger.debug("Instance getInstance " + instance.getId());
return instance;
}
这永远不会奏效,您正在开发 Web 应用程序。
这意味着2个用户同时只能有1个实例,但是你的文章分配到特定的桌面,所以1个用户会有问题。
有什么问题:
lb.setItemRenderer(new ArticleItemRenderer(tb_article_abstract));
您的渲染器有这个特定 class 的新实例,因此它绑定到正确的桌面。
只要您不从 DOM 中删除该项目,就没有问题。
我在访问 ZK 项目中模型 class 之外的 GUI 元素时遇到问题。我有一个 ListItemRenderer,它在我单击列表框中的元素时呈现数据和相应模型的位置,但是当我再次访问文章 class 以及所有 GUI 元素时,我的实例永久为空我第一次创建 class 时连接(通过 getFellow ())。这甚至是正确的方法吗?或者,我可以向变量添加一个侦听器,以便在更改文本框时重新加载该变量吗?感谢您的每一个提示,我已经经历了几个令人沮丧的小时,希望代码片段足够了。
祖尔:
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" root="./winArticles"?>
<window id="winArticles" title="Looking for Evidence" width="1100px" height="880px"
border="normal" use="xxx.Articles" closable="true" sizable="false">
...
<listbox id="artikel_output" width="100%" height="" multiple="true"
checkmark="true" vflex="true" nonselectableTags="*">
<listhead>
<listheader label="Artikel"/>
</listhead>
</listbox>
...
<div vflex="1">
<textbox style="font-size:50px"
id="tb_article_abstract"
multiline="true"/>
</div>
...
</window>
型号:
public class Articles extends Window implements AfterCompose,
IGenericDomainValueSelectorActions, IUpdateModal, IGenericListActions {
private static Articles instance;
public Articles() { }
public static Articles getInstance() {
if (instance == null) {
instance = new Articles();
}
logger.debug("Instance getInstance " + instance.getId());
return instance;
}
@Override
public void afterCompose() {
instance = new Articles();
tb_article_abstract = (Textbox) getFellow("tb_article_abstract");
...}
正在初始化列表框渲染器:
public void fillListBoxWithArticles(final Listbox lb, List<Article> articles) {
if (articles.size() == 0) {
lb.appendChild((Component) articles.get(0));
}
lb.setItemRenderer(new ArticleItemRenderer());
lb.setCheckmark(true);
lb.setMultiple(true);
ListModelList<Article> lml_articles = new ListModelList<Article>(articles);
lml_articles.setMultiple(true);
lb.setModel(lml_articles);
lb.focus();
}
如果选择了列表框元素,在文本框中显示它的属性,但文本框始终为空...
public void setArticleToInfobox(Article selectedArticle, int selectedIndex) {
this.selectedArticle = selectedArticle;
// tb_article_abstract = null
tb_article_abstract.setValue(selectedArticle.getAbstract_full());
tb_article_abstract.invalidate();
}}
ItemRenderer 正在获取所选项目的数据(和索引):
public class ArticleItemRenderer implements ListitemRenderer<Article> {
@Override
public void render(Listitem item, Article data, int index) throws Exception {
item.appendChild(new Listcell(data.getArticletitle() + ", " );
item.addEventListener(Events.ON_CLICK, new EventListener() {
@Override
public void onEvent(Event event) throws Exception {
EvidenceBasedArticles.getInstance().setArticleToInfobox(data, item.getIndex());
}});}
你在这里犯了一些严重的错误。
public static Articles getInstance() {
if (instance == null) {
instance = new Articles();
}
logger.debug("Instance getInstance " + instance.getId());
return instance;
}
这永远不会奏效,您正在开发 Web 应用程序。
这意味着2个用户同时只能有1个实例,但是你的文章分配到特定的桌面,所以1个用户会有问题。
有什么问题:
lb.setItemRenderer(new ArticleItemRenderer(tb_article_abstract));
您的渲染器有这个特定 class 的新实例,因此它绑定到正确的桌面。
只要您不从 DOM 中删除该项目,就没有问题。