GWT UiHandler on HTMLPanel
Asked Answered
L

3

7

I'm writing a widget with the following markup:

<g:HTMLPanel ui:field="shortcutPanel" styleName="{style.shortcut}">
    <g:Image ui:field="shortcutImage"></g:Image>
    <span ui:field="shortcutLabel"></span>
</g:HTMLPanel>

So essentially a div that wraps and image and a label. Now, instead of adding the event handlers on the image/span, I'd like an onClick to be associated with the HTMLPanel. My problem however is that gwt tells me that

shortcutPanel doesn't not have an addClickHandler method associated

So I'm assuming the difference is that HTMLPanel doesn't implement HasClickHandlers or something along that line. I'm wondering then what is the standard way to attach a click handler to a Ui element such as an HTMLPanel or even better, is there such a GWT Widget that is essentially a div wrapper that I can easily attach events to with the @UiHandler annotation.

Lecialecithin answered 6/4, 2010 at 19:14 Comment(0)
I
21

You are probably looking for FocusPanel - it has all the goodies: HasAllFocusHandlers, HasAllKeyHandlers, HasAllMouseHandlers, HasBlurHandlers, HasClickHandlers.... to name a few :) I find it to be the easiest and best way to attach click handlers to a Panel.

Insomuch answered 6/4, 2010 at 22:14 Comment(1)
That worked Thanks! I had to modify my markup to have a FlowPanel directly under the FocusPanel that wraps the label and image, since FlowPanel is a SimplePanel and can only have one widget. Other than that, worked like a charm. My only beef is that it produces a bit of extra, unnecessary markup which I'm not a fan of, but I'm quickly realizing that people don't seem to care about that in the GWT world.Lecialecithin
T
7

I haven't done this before, but you could do the following:

  • Create a custom class MyPanel that extends HTMLPanel and implements HasClickHandlers
  • Add the following method in MyPanel.java
    public HandlerRegistration addClickHandler(ClickHandler handler) {
        return addDomHandler(handler, ClickEvent.getType());
    }
  • Then replace HTMLPanel with MyPanel in your ui.xml and its corresponding Java implementation.

You can always look at the implementation of HTMLTable to get an understanding of how the event propagation works. It's a Panel and implements HasClickHandlers.

Tampere answered 6/4, 2010 at 19:32 Comment(0)
R
1

If you want to use the @UiHandler annotation to register event handlers for your custom widget, you need to re-implement the addXXHandler methods. The GWT compiler doesn't seem to find those in superclasses. e.g. if you want to use

@UiHandler("myCustomWidget")
public void handleWidgetSelectionChangeEvent(final SelectionEvent<CountryDts> event) {
...
}

and your CustomWidget extends a class for which this is working, you might need to add the HasSelectionHandlers interface explicitly to your class:

public class CustomComboBox<D> extends ComboBox<D> implements HasSelectionHandlers<D> {

    @Override
    @SuppressWarnings("pmd.UselessOverridingMethod")
    public HandlerRegistration addSelectionHandler(final SelectionHandler<D> handler) {
        // GWT Compile doesn't recognize method in supertype for UIHandler
        return super.addSelectionHandler(handler);
    }
...
}
Rescript answered 7/11, 2012 at 16:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.