<...">

GWT UiHandler on HTMLPanel

I am 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 the div that wraps, as well as the image and label. Now, instead of adding event handlers to the / span image, I would like onClick to be associated with an HTMLPanel. My problem, however, is that gwt tells me that

shortcutPanel does not support addClickHandler method

Therefore, I assume that the difference is that the HTMLPanel does not implement HasClickHandlers or something in that direction. I am wondering what a standard way to bind a click handler to a Ui element, such as an HTMLPanel, or even better, is there a GWT widget that is essentially a wrapper div with which I can easily attach events to @UiHandler annotations.

+6
event-handling gwt uibinder
source share
4 answers

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

+20
source share

I have not done this before, but you could do the following:

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

You can always look at the implementation of HTMLTable to understand how event propagation works. It is a Panel and implements HasClickHandlers .

+7
source share

excellent, it works.

 public class LiPanel extends HTMLPanel implements HasClickHandlers { LiPanel() { super("li", ""); } @Override public HandlerRegistration addClickHandler(ClickHandler handler) { return addDomHandler(handler, ClickEvent.getType()); } } 
+2
source share

If you want to use the @UiHandler annotation to register event handlers for your custom widget, you need to reimplement the addXXHandler methods. The GWT compiler does not seem to find them in superclasses. for example if you want to use

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

and your CustomWidget extends the class for which it works, you may 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); } ... } 
+1
source share

All Articles