S Jagdeesh
S Jagdeesh

Reputation: 1553

GWT clickHandler - Condition failing

I have a click handler method defined in a class. I am trying to call a particular method if a cancel button on the screen is clicked. here's the code snippet -

    boolean tempCheck;

    @Override
    protected void onBind() {

    super.onBind();

    ...
    ...

    getViewName().getVar().addClickHandler(new ClickHandler() {

    @Override
    public void onClick(final ClickEvent event) {

    //Doing some operation and making tempCheck true

    tempCheck = true;       

    }
    });


    If(tempCheck){

    this.box.getButtonName().addClickHandler(new ClickHandler() {
        @Override
        public void onClick(final ClickEvent event) {

        this.box.hide();
        this.getViewName().hide();
    }
    });

    } else {


    this.callToMethodA();

    }

}

When the button gets clicked, tempCheck boolean variable becomes true.

tempCheck = true;

but if condition is getting failed, it always go into else part.

If(tempCheck)

why this is happening? is this because of how java managed closures? Note : If condition has to be outside the block as this.callToMethodA() is used by other click handlers too.

Upvotes: 2

Views: 154

Answers (2)

Azeez
Azeez

Reputation: 328

Your way of thinking about Event Handling is wrong.....You have to write the functionality "in side the onClick() itself,whichever you want to execute when you click the close button. But here what you are doing is just changing one variable value...you are not doing the functionality you want to do inside the onClick() I hope u understand....Even if u didn't understand this....think once...you can come to know your silly mistake...

Upvotes: 0

David Levesque
David Levesque

Reputation: 22441

When you call addClickHandler(), it creates the handler and immediately continues with the next statement (which is If(tempCheck) in this case). It does not wait for the button to be clicked, as you seem to assume. So tempCheck will always be false at this point.

Any code you want executed after the button is clicked has to go inside the onClick() method, or inside a method that you call from onClick().

Upvotes: 3

Related Questions