将数据从一个作曲家发送到另一个

时间:2018-07-12 09:32:20

标签: java zk zul

我有两个zul:index.zulpopup.zul

index.zul显示一个按钮,该按钮打开一个窗口onClick。在该窗口中,popup.zul显示用户可以在其中输入值的文本框。用户单击确认按钮后,窗口将关闭。

如何将输入的值返回到index.zul以便在那里显示?

IndexComposer.java

public class IndexComposer extends SelectorComposer<Component> {
   private static final long serialVersionUID = 1L;

   @Wire
   private Window popupWindow;

   @Listen("onClick = #btn")
   public void button() {
       Window win = (Window) Executions.createComponents("popup.zul", 
                                                         getSelf().getParent(),
                                                         null);
       win.doModal();
   }
}

index.zul

<zk>
   <div apply="co.id.test.master.test.IndexComposer">
      <label id="lbl" />
      <button id="btn" label="Open popup" />
   </div>
</zk>

PopupComposer.java

public class PopupComposer extends SelectorComposer<Component> {
    private static final long serialVersionUID = 1L;

    @Wire
    private Window popupWindow;

    @Wire
    private Textbox txt;

    @Listen("onClick = #close")
    public void closePopup() {
        System.out.println(txt.getValue());
        popupWindow.detach();
    }
}

popup.zul

<zk xmlns:h="http://www.w3.org/1999/xhtml">
   <window id="popupWindow" title="Coffee Order" border="normal" mode="modal">
       <div apply="co.id.test.master.test.PopupComposer">
           <textbox id="txt" />
           <button id="close" label="close" />
       </div>
   </window>
</zk>

2 个答案:

答案 0 :(得分:0)

一种实现方法是使用EventQueues。它们是一种在应用程序的不同部分之间发送事件的方式,而无需具有发送者和接收者必须知道的目标组件(例如使用“正常” Events.sendEvent())。

这是一个入门的简单示例。请注意,我尚未对此进行测试,并且我使用了APPLICATION范围,这是一种核选择。例如,DESKTOP就足够了。 Please read about EventQueues a bit

public class IndexComposer
    extends SelectorComposer<Component>
{
    public IndexComposer()
    {
        EventQueues.lookup("MyEventQueueName", EventQueues.APPLICATION, true)
                   .subscribe(e -> {
                       String data = (String) e.getData();
                       // update your UI element
                   });
    }

    @Listen("onClick = #btn")
    public void button()
    {
        Window win = (Window) Executions.createComponents("popup.zul", getSelf().getParent(), null);
        win.doModal();
    }
}

public class PopupComposer
    extends SelectorComposer<Component>
{
    @Wire
    private Window  popupWindow;

    @Wire
    private Textbox txt;

    @Listen("onClick = #close")
    public void closePopup()
    {
        popupWindow.detach();
        EventQueues.lookup("MyEventQueueName", EventQueues.APPLICATION, true)
                   .publish(new Event("", null, txt.getValue()));
    }
}

答案 1 :(得分:-2)

是的,以zk格式获取文档 关于事件监听