Search code examples
javaoopgwtuser-interfaceuibinder

Handling GWT Click Events In a Different Method


Basically what I am trying to do here is handle click events which panel is suppose to appear to depending on which button is click. For example, if we click button one, the corresponding panel will pop up. But the panel and click event does not know anything about each other. I believe its called anonymous class. I am having trouble trying to implement this. What would be a good way to implement this?

This is my button click event class

public class buttonHandle extends Composite {

private static buttonHandleUiBinder uiBinder = GWT
        .create(buttonHandleUiBinder.class);
@UiField Button button;
@UiField Button button_1;

interface buttonHandleUiBinder extends UiBinder<Widget, buttonHandle> {
}
public buttonHandle() {
    initWidget(uiBinder.createAndBindUi(this));
}

@UiHandler("button")
void onButtonClick(ClickEvent event) {
}
@UiHandler("button_1")
void onButton_1Click(ClickEvent event) {
}
}

This is the class where I am trying to add a new button everytime a button is clicked

public class PanelHandle extends Composite {
private AbsolutePanel absolutePanel = new AbsolutePanel();

public PanelHandle() {

    initWidget(absolutePanel);
    absolutePanel.setSize("1027px", "636px");

    Label lblHello = new Label("Hello");
    absolutePanel.add(lblHello, 47, 80);

    Label lblHello_1 = new Label("Hello");
    absolutePanel.add(lblHello_1, 232, 249);
    // TODO Auto-generated constructor stub
}
public void buttonOne()
{
    this.absolutePanel.clear();
    Button but1 = new Button("button one");
    this.absolutePanel.add(but1);

}
}

I tried something like this, but it does not update the panel with a new button

private PanelHandle pHandle = new PanelHandle();
private static buttonHandleUiBinder uiBinder = GWT
        .create(buttonHandleUiBinder.class);
@UiField Button button;
@UiField Button button_1;

interface buttonHandleUiBinder extends UiBinder<Widget, buttonHandle> {
}
public buttonHandle() {
    initWidget(uiBinder.createAndBindUi(this));
}


@UiHandler("button")
void onButtonClick(ClickEvent event) {
    Window.alert("hello buttone clicked");
    pHandle.buttonOne();


}
@UiHandler("button_1")
void onButton_1Click(ClickEvent event) {
}
}

So far I tried to call the method in my PanelHandle class, but I am encountering errors such stack overflow. In another method I tried, I am unable to update the panel when I add. I am using a button here instead of panel just for testing until I understand the logic.

Thank You for you help!


Solution

  • Create an own (gwt)event when a button is clicked. You can fire your own event wherever you want. Fill the event with the information you need. Next add the class which have to handle this event to the gwt eventbus. If a event is fired, your handle class catch the event and work with the data from the event.

    This could be helpful: How to use the GWT EventBus