Add a click handler to a HorizontalPanel in GWT

巧了我就是萌 提交于 2019-12-04 19:22:53

问题


How do i add click handlers to HorizontalPanel?

It worked with the use of addDomHandler() in newer GWT versions, but i had to downgrade to GWT 2.0.4 where this isn't supported. I used to do it like this:

horizontalPanel.getWidget(1).addDomHandler(someClickHandler,ClickEvent.getType());
//or
horizontalPanel.addDomHandler(someClickHandler, ClickEvent.getType());

回答1:


Use FocusPanels instead of hooking native events. To catch clicks for the whole panel:

FocusPanel wrapper = new FocusPanel();
HorizontalPanel panel = new HorizontalPanel();
wrapper.add(panel);
wrapper.addClickHandler(new ClickHandler() {
  @Override
  public void onClick(ClickEvent event) {
    // Handle the click
  }
});

// Add wrapper to the parent widget that previously held panel.

Or to catch clicks inside a cell in the HorizontalPanel:

IsWidget child; // Any widget
HorizontalPanel panel = new HorizontalPanel();
FocusPanel clickBox = new FocusPanel();

clickBox.add(child);
panel.add(clickBox);

clickBox.addClickHandler(...);


来源:https://stackoverflow.com/questions/4942140/add-a-click-handler-to-a-horizontalpanel-in-gwt

工具导航Map