How do I setup for and/or handle onload event with GWT

12,607

Most widgets and panels in GWT implements the HasAttachHandlers interface. Adding an AttachEvent.Handler to these widgets/panels is equivalent to defining a function to run onload.

An example:

FlowPanel mainPanel = new FlowPanel();
mainPanel.addAttachHandler(new AttachEvent.Handler() {

  @Override
  public void onAttachOrDetach(AttachEvent event) {
    // do something
  }
});
Share:
12,607
Justin
Author by

Justin

Working hard to not be labeled an idiot and failing.

Updated on June 17, 2022

Comments

  • Justin
    Justin almost 2 years

    This has to be a pretty basic question but I cannot for the life of me find anything via Google or this site about how to do this and it's really frustrating because I've gone through the GWT tutorial.

    I'm trying to convert a javascript project I have to GWT. Right now I am trying to convert "" to something GWT equivalent. I've looked at RootPanel and I can't see anything.

    Obviously I'm missing something fundamental in GWT ?!

  • Justin
    Justin about 13 years
    I'm a pretty big idiot; is there a way you can provide an example? After reading your response and pouring through the API as well as Google searching the best I've come up with (which is completely wrong according to the red underlines in Eclipse) is: mainPanel.addHandler(new LoadHandler(){public onload(LoadEvent event);}, DomEvent<EventHandler>.class);
  • Justin
    Justin about 13 years
    UPDATE(I'm trying real hard to use stackoverflow.com/editing-help I promise) I think I may have it Can you tell me if this seems reasonable mainPanel.addHandler(new LoadHandler() { public void onLoad(final LoadEvent event) { } }, LoadEvent.getType());
  • smallbec
    smallbec about 13 years
    You would do something like this, assuming your mainPanel is a FlowPanel: FlowPanel mainPanel = new FlowPanel(); mainPanel.addAttachHandler(new AttachEvent.Handler() { @Override public void onAttachOrDetach(AttachEvent event) { // do something } });
  • Justin
    Justin about 13 years
    Thanks, that did it. Guess the "attach" jargon didn't make sense at first.