1

I am using gwt for developing one UI and the requirement there is that in case of some actions on the child widgets, the parent widget needs to take some action corresponding to that.

For instance, i have a main panel (the parent pane) which has a list of child widgets. Based on the changes to any of the child widgets, other widget contents need to be updated.

What is the correct design to follow in this case,i.e, 1 option is: - child catches the change event and updates itself. Raises an event to the parent widget which then updates the child widgets. - Other alternative is to have the change handler for the child component in the parent itself and parent can then propagate the changes to the other components.

karan
  • 61
  • 2

3 Answers3

1

You only need one event handler. For example:

ClickHandler h = new ClickHandler() {
    public void onClick(ClickEvent event) {
        Widget sender = (Widget) event.getSource();

             if (sender == child1) {
                // update child1
                // update child 2
             } else if (sender == child2) {
                // update child2
                // update child1
             }
        }
    }
}
parent.addDomHandler(h, ClickEvent.getType());
Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58
0

I think the best choice would be to use EventBus. Simple explanation how use it : http://tv.jetbrains.net/videocontent/gwt-event-bus-basics . First - define the events for child widgets. And Use them by EventBus. Look to this answer: https://stackoverflow.com/a/6030379/904820

Community
  • 1
  • 1
Hussar
  • 89
  • 2
  • 4
  • I would not recommend EventBus for such a simple task. EventBus is best used when you have events that happen in one view and you want to update other views, and you don't want to pass a myriad of event handlers between them. – Andrei Volgin Oct 14 '12 at 16:02
0

You can make use of UiHandlers of Child as well as Parent Widgets. Please go through the link. https://developers.google.com/web-toolkit/doc/latest/DevGuideUiHandlers

PVR
  • 2,534
  • 18
  • 38