Message Box

From Documentation
Revision as of 05:10, 3 September 2010 by Tomyeh (talk | contribs)

Stop.png This documentation is for an older version of ZK. For the latest one, please click here.


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.

Message Boxes with Event Thread 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
 }



Last Update : 2010/09/03

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.