Message Box"
(Created page with '{{ZKDevelopersGuidePageHeader}} The message boxes returns immediately so it always returns <tt>Messagebox.OK</tt>. Thus, it is meaningless to show buttons other than the OK butt…') |
|||
(10 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
− | {{ | + | {{ZKDevelopersReferencePageHeader}} |
− | + | = Message Boxes with Servlet Thread = | |
+ | |||
+ | When <javadoc method="show(java.lang.String)">org.zkoss.zul.Messagebox</javadoc> is called, it returns immediately after showing the message dialog. Furthermore, it always returns <code>Messagebox.OK</code>. Thus, it is meaningless to show buttons other than the OK button. For example, the <code>if</code> clause in the following example is never true. | ||
<source lang="java" > | <source lang="java" > | ||
Line 26: | Line 28: | ||
</source> | </source> | ||
− | The event listener you provided is invoked when the user clicks one of the buttons. Then, you can identify which button is clicked by examining the data (Event's < | + | The event listener you provided is invoked when the user clicks one of the buttons. Then, you can identify which button is clicked by examining the data (Event's <code>getData</code>). The data is an integer whose value is the button's identifier, such as <code>Messagebox.YES</code>. |
Alternatively, you can examine the event name: | Alternatively, you can examine the event name: | ||
Line 40: | Line 42: | ||
</source> | </source> | ||
− | '''Note''': The event name for the OK button is < | + | '''Note''': The event name for the OK button is <code>onOK</code>, not <code>onOk</code>. |
+ | '''Notice''': If you want to make it run under clustering environment, you shall implement <javadoc type="interface">org.zkoss.zk.ui.event.SerializableEventListener</javadoc>. For more information, please refer to [[ZK Developer's Reference/Clustering/Programming Tips|ZK Developer's Reference: Clustering]]. | ||
+ | |||
+ | = Message Boxes with Event Thread = | ||
+ | |||
+ | If the event thread is enabled, <javadoc method="show(java.lang.String)">org.zkoss.zul.Messagebox</javadoc> will suspend the thread until the end user makes the choice. Thus, the following code works correctly. | ||
+ | |||
+ | <source lang="java" > | ||
+ | if (Messagebox.show("Delete?", "Prompt", Messagebox.YES|Messagebox.NO, | ||
+ | Messagebox.QUESTION) == Messagebox.YES) { | ||
+ | //execute only if the YES button is clicked | ||
+ | } | ||
+ | </source> | ||
+ | |||
− | {{ | + | {{ZKDevelopersReferencePageFooter}} |
Latest revision as of 09:33, 7 September 2023
Message Boxes with Servlet Thread
When Messagebox.show(String) is called, it returns immediately after showing the message dialog. Furthermore, it always returns Messagebox.OK
. Thus, it is meaningless to show buttons other than the OK button. For example, the if
clause in the following example is never true.
if (Messagebox.show("Delete?", "Prompt", Messagebox.YES|Messagebox.NO,
Messagebox.QUESTION) == Messagebox.YES) {
this_never_executes();
}
Rather, you have to provide an event listener as follows.
Messagebox.show("Delete?", "Prompt", Messagebox.YES|Messagebox.NO,
Messagebox.QUESTION,
new EventListener() {
public void onEvent(Event evt) {
switch (((Integer)evt.getData()).intValue()) {
case Messagebox.YES: doYes(); break; //the Yes button is pressed
case Messagebox.NO: doNo(); break; //the No button is pressed
}
}
}
);
The event listener you provided is invoked when the user clicks one of the buttons. Then, you can identify which button is clicked by examining the data (Event's getData
). The data is an integer whose value is the button's identifier, such as Messagebox.YES
.
Alternatively, you can examine the event name:
public void onEvent(Event evt) {
if ("onYes".equals(evt.getName())) {
doYes(); //the Yes button is pressed
} else if ("onNo".equals(evt.getName())) {
doNo(); //the No button is pressed
}
}
Note: The event name for the OK button is onOK
, not onOk
.
Notice: If you want to make it run under clustering environment, you shall implement SerializableEventListener. For more information, please refer to ZK Developer's Reference: Clustering.
Message Boxes with Event Thread
If the event thread is enabled, Messagebox.show(String) will suspend the thread until the end user makes the choice. Thus, the following code works correctly.
if (Messagebox.show("Delete?", "Prompt", Messagebox.YES|Messagebox.NO,
Messagebox.QUESTION) == Messagebox.YES) {
//execute only if the YES button is clicked
}