将数据从一位作曲家发送到另一位作曲家
Send data from one composer to another
我有两个 zul 的申请:index.zul
和 popup.zul
。
index.zul
显示一个按钮,它打开一个 window onClick。在 window、popup.zul
中显示了一个文本框,用户可以在其中键入一个值。一旦用户单击确认按钮,window 就会关闭。
如何将输入的值返回到 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>
一种方法是使用 EventQueues
。它们是一种跨应用程序的不同部分发送事件的方法,而无需具有发送方和接收方必须知道的目标组件(例如 "normal" 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()));
}
}
是的,在 zk 中获取文档
关于事件监听
我有两个 zul 的申请:index.zul
和 popup.zul
。
index.zul
显示一个按钮,它打开一个 window onClick。在 window、popup.zul
中显示了一个文本框,用户可以在其中键入一个值。一旦用户单击确认按钮,window 就会关闭。
如何将输入的值返回到 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>
一种方法是使用 EventQueues
。它们是一种跨应用程序的不同部分发送事件的方法,而无需具有发送方和接收方必须知道的目标组件(例如 "normal" 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()));
}
}
是的,在 zk 中获取文档 关于事件监听