将点击处理程序添加到 GWT 中的 Horizo​​ntalPanel

2022-01-09 00:00:00 events onclick gwt java

如何将点击处理程序添加到 Horizo​​ntalPanel?

How do i add click handlers to HorizontalPanel?

它可以在较新的 GWT 版本中使用 addDomHandler(),但我不得不降级到不支持此功能的 GWT 2.0.4.我以前是这样的:

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());

推荐答案

使用 FocusPanels 而不是挂钩原生事件.捕获整个面板的点击:

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.

或者在 Horizo​​ntalPanel 的单元格内捕捉点击:

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(...);

相关文章