Clickhandler不会触发

时间:2012-10-19 09:59:14

标签: java gwt

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

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

  1. 我创建了一个新的插入方法:addToTitle(...)
  2. 我在按钮
  3. 中添加了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); 
        } 
    }
    

2 个答案:

答案 0 :(得分: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();
    }
}

就是这样。

答案 1 :(得分:0)

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

yourWidget.addDomHandler(new ClickHandler() {

  @Override
  public void onClick(ClickEvent event) {


  }
},ClickEvent.getType());