Question

I have different 3 Different Buttons with different onclick events :

add.addClickHandler(new ClickHandler()
        {

            @Override
            public void onClick(ClickEvent event)
            {
                add();
            }
        });

set.addClickHandler(new ClickHandler()
        {

            @Override
            public void onClick(ClickEvent event)
            {
                set();
            }
        });

get.addClickHandler(new ClickHandler()
        {

            @Override
            public void onClick(ClickEvent event)
            {
                get();
            }
        });

So now if i extend this up to 10 Buttons my Script would be far to long, is there a way to pass the Methode or do seperate the Handlers?

Was it helpful?

Solution

Suppose you have some view:

customview.ui.xml

<g:VerticalPanel>
    <style:Button ui:field="addButton" text="Add"/>
    <style:Button ui:field="setButton" text="Set"/>
    <style:Button ui:field="getButton" text="Get"/>
</g:VerticalPanel>

In your View class define 3 fields and 3 handlers:

CustomView.java

public class CustomView extends ViewWithUiHandlers<CustomUiHandlers>
    implements CustomPresenter.MyView {

    @UiField
    Button addButton;

    @UiField
    Button setButton;

    @UiField
    Button getButton;

    // Here constructor and other code

    @UiHandler("addButton")
    void onAddButtonClicked(ClickEvent event) {
    if (getUiHandlers() != null) {
        getUiHandlers().onAddClicked();
    }
}

    @UiHandler("setButton")
    void onSetButtonClicked(ClickEvent event) {
    if (getUiHandlers() != null) {
        getUiHandlers().onSetClicked();
    }
}

    @UiHandler("getButton")
    void onGetButtonClicked(ClickEvent event) {
    if (getUiHandlers() != null) {
        getUiHandlers().onGetClicked();
    }
}
}

CustomUiHandlers.java

public interface CustomUiHandlers extends UiHandlers {
    void onAddClicked();
    void onSetClicked();
    void onGetClicked();
}

CustomPresenter.java

public class CustomPresenter extends
    Presenter<CustomPresenter.MyView, CustomPresenter.MyProxy>
            implements CustomUiHandlers {

    // Some code

    @Override
    public void onAddClicked() {
    // Here your code
    }

    @Override
    public void onSetClicked() {
    // Here your code
    }

    @Override
    public void onGetClicked() {
    // Here your code
    }
}

OTHER TIPS

You can bind event handler to a method by UiBinder, or alternatively wait for lambda support for GWT.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top