1

ClickHandler在使用 GWT 的项目中遇到问题。

在对话框的标题中,我想插入一个新按钮。

  1. 我创建了一个新的插入方法:addToTitle(...).
  2. 我添加ClickHandler到按钮

问题:按钮的单击事件不会触发。为什么?

这是我的代码:

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

代码(摘自评论部分):

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); 
    } 
}
4

2 回答 2

0

如果您唯一的问题是没有调用 ClickHandler,请尝试使用 addDOMHandler 而不是 addClickHandler

yourWidget.addDomHandler(new ClickHandler() {

  @Override
  public void onClick(ClickEvent event) {


  }
},ClickEvent.getType());
于 2013-11-28T11:36:24.230 回答
0

解决方案有点棘手。

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

就这样。

于 2012-10-20T14:30:24.083 回答