简体   繁体   English

Clickhandler不会触发

[英]Clickhandler doesn't fire

I have a problem with ClickHandler in my project using GWT. 我在使用GWT的项目中ClickHandler出现问题。

In the title of dialog box I want to insert a new button. 我想在对话框的标题中插入一个新按钮。

  1. I created a new insert method: addToTitle(...) . 我创建了一个新的插入方法: addToTitle(...)
  2. I added ClickHandler to the button 我在按钮上添加了ClickHandler

Problem: click event by button doesn't fire. 问题:按按钮单击事件不会触发。 Why? 为什么?

Here is my code: 这是我的代码:

DialogBox dialog = new DialogBox();

Button button = new Button("A new Button");

       button.addClickHandler(new ClickHandler()
        {
            @Override
            public void onClick(ClickEvent event)
            {
                Window.alert("yuhuhuhu");

            }
        });

dialog.addToTitle(button);

code (extracted from the comments section) : 代码(从注释部分中提取):

public class PlentyDialogWindow extends DialogBox { 
    private FlowPanel captionPanel = new FlowPanel(); 
    public Widget closeWidget = null; 
    private boolean closeOnEscKey = false; 
    private FlowPanel titleContentWrapper = new FlowPanel(); 
    public PlentyDialogWindow(boolean isModal) { 
        super( false, isModal); 
        this.addStyleName("DialogBox"); 
        this.getElement().setId("DialogBoxId"); 
        this.setAnimationEnabled(true); 
        this.closeWidget = generateCloseButton(); 
    }

    public void setCaption( String txt,Widget w) { 
        captionPanel.setWidth("100%"); 
        this.addCaption(txt); 
        this.titleContentWrapper.getElement().getStyle().setDisplay(Display.INLINE_BLOCK); 
        captionPanel.add(this.titleContentWrapper); 
        FlowPanel widgetWrapper = new FlowPanel(); 
        widgetWrapper.add(w); 
        widgetWrapper.addStyleName("PlentyPopupCloseIconWrapper"); 
        captionPanel.add(widgetWrapper); 
        captionPanel.addStyleName("Caption"); 
        Element td = getCellElement(0,1); 
        td.setInnerHTML(""); 
        td.appendChild(captionPanel.getElement()); 
    }

    /** * * @param w */ public void addToTitle(Widget w) { 
        this.titleContentWrapper.add(w); 
    } 
}

The solution is a bit tricky. 解决方案有点棘手。

public class PlentyDialogWindow extends DialogBox {

    /* 
     * Create custom inner class extending `FlowPanel`. You need it only
     * to make `onAttach` and `onDetach` methods be visible to wrapping 
     * class (e.g. your `PlentyDialogWindow` class).
     */
    static class MyCaptionPanel extends FlowPanel {

        @Override
        protected void onAttach() {
            super.onAttach();
        }

        @Override
        protected void onDetach() {
            super.onDetach();
        }
    }

    /* 
     * `PlentyDialogWindow`'s field `captionPanel` will be an instance of 
     * this class.
     */
    private MyCaptionPanel captionPanel = new MyCaptionPanel();


    /*
     * ... leave the rest of your class untouched ...
     */


    /*
     * Finally, overwrite `PlentyDialogWindow`'s `onAttach` and `onDetach` 
     * methods to invoke `captionPanel`'s corresponding methods:
     */
    @Override
    protected void onAttach() {
        super.onAttach();
        captionPanel.onAttach();
    }

    @Override
    protected void onDetach() {
        super.onDetach();
        captionPanel.onDetach();
    }
}

That's all. 就这样。

If your only problem is ClickHandler not being called try using addDOMHandler instead of addClickHandler 如果您唯一的问题是没有调用ClickHandler,请尝试使用addDOMHandler而不是addClickHandler

yourWidget.addDomHandler(new ClickHandler() {

  @Override
  public void onClick(ClickEvent event) {


  }
},ClickEvent.getType());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM