Lee
Lee

Reputation: 353

How to implement GWT, Java, button, and the clickhandler

Forgive me if this question has been asked and answered, as I have been unable to find it if it has.

I can find several examples like

button.addClickHandler(new ClickHandler() {
    public void onClick(ClickEvent event) {
        ... stuff ...
    }
});

I'm trying to understand how to implement a button click handler using the following structure.

public class myClass implements EntryPoint {
    final Button MyButton = new Button("text");
        :
        :
    void onClickMyButton(???) {
            ... stuff ...
    }
        :
        :
}

To "me", this structure is more easily read and is just my preference in coding style. But I don't know how to implement it.

I'm using Eclipse and GWT for a Java Web App. Any help would be appreciated.

Upvotes: 1

Views: 976

Answers (1)

JB Nizet
JB Nizet

Reputation: 692231

Either

public class myClass implements EntryPoint, ClickHandler {
    final Button myButton = new Button("text");
        :
        :
    myButton.addClickHandler(this);


    @Override
    void onClick(ClickEvent event) {
            ... stuff ...
    }
}

or

public class myClass implements EntryPoint {
    final Button myButton = new Button("text");
        :
        :
    myButton.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            onClickMyButton(event);
        }
    });

    private void onClickMyButton(ClickEvent event) {
            ... stuff ...
    }
}

The second one is much cleaner, and allows handling several buttons with separate methods.

Upvotes: 2

Related Questions