【问题标题】:ZK - Accessing GUI elements from ItemRendererZK - 从 ItemRenderer 访问 GUI 元素
【发布时间】:2018-03-10 23:46:39
【问题描述】:

我在访问 ZK 项目中模型类之外的 GUI 元素时遇到问题。我有一个 ListItemRenderer,当我单击列表框中的元素时,它会呈现数据和相应模型的位置,但是当我再次访问文章类以及我连接的所有 GUI 元素时,我的实例永久为空我是第一次创建这个类(通过 getFellow())。这甚至是正确的方法吗?或者,我可以向一个变量添加一个侦听器,如果它被更改,它会重新加载文本框?感谢每一个提示,我已经经历了几个令人沮丧的小时,希望代码 sn-p 就足够了。

祖尔:

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

【问题讨论】:

    标签: java textbox zk itemrenderer


    【解决方案1】:

    你在这里犯了一些关键的错误。

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

    您的渲染器有一个用于该特定类的新实例,因此它已绑定到正确的桌面。
    只要您不从 DOM 中删除该项目,就没有问题。

    【讨论】:

    • 真丢脸,我想很明显我是开发 Web 应用程序的新手 ;)。非常感谢您可以理解的解释,工作正常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 2014-10-23
    相关资源
    最近更新 更多