Search code examples
javazk

ZK Message Box Confirmation


I'm using ZK and found some strange behavior. The code:

@Listen("onClick = button#load")
public void load() {
    int result = Messagebox.show("Are you sure to execute Load?", "Execute?",
            Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);
    log.debug("Result: {}", result);
    if (result == Messagebox.YES) {
        (new Thread(new Job("Load"))).start();
        message.setValue("Job " + "Load " + " is Executed at " + new Date());

        log.info("Load Called");
    } else {
        log.debug("Load Not Called");
    }
}

Either Yes or No is Clicked, it return 1 as int. Logs:

 02-Oct-2016 23:59:31.725 FINE [http-nio-8080-exec-1] com.Controller.load Result: 1
 02-Oct-2016 23:59:31.726 FINE [http-nio-8080-exec-1] com.Controller.load Load Not Called
 02-Oct-2016 23:59:39.541 FINE [http-nio-8080-exec-6] com.Controller.load Result: 1
 02-Oct-2016 23:59:39.542 FINE [http-nio-8080-exec-6] com.Controller.load Load Not Called

How to make it right?


Solution

  • Which version of ZK are you using?

    The javadoc says :

    @return the button being pressed (one of {@link #OK}, {@link #CANCEL}, {@link #YES}, {@link #NO}, {@link #ABORT}, {@link #RETRY}, and {@link #IGNORE}).

    Note: if the event processing thread is disabled, it always returns {@link #OK}.

    But if you check the documentation about the event processing thread, it says :

    [Since ZK 7.0.0 deprecated to enable the event thread according to Java Servlet Specification that may prohibit the creation of new threads]

    So should notify ZK to update its javadoc as if you are using ZK7 or 8 the method will always return 1 right away.

    To answer your question, if you want to call specific action depending on which button is clicked:

    @Listen("onClick = button#load")
    public void load() {
        Messagebox.show("Are you sure to execute Load?", "Execute?", Messagebox.YES | Messagebox.NO, 
            Messagebox.QUESTION, new EventListener<Event>() {
                @Override
                public void onEvent(final Event evt) throws InterruptedException {
                    if (Messagebox.ON_YES.equals(evt.getName())) {
                        // Code if yes clicked
                    } else {
                        // Code if no clicked
                    }
                }
            }
        );
    }