Firing click event from code in gwt

后端 未结 8 665
甜味超标
甜味超标 2020-12-03 10:27

I have created a custom widget in gwt which extends the composite.I am using focus panel in that.For FocusPanel I added ClickHandler.Then I have added keyboard listner.Now o

相关标签:
8条回答
  • 2020-12-03 10:50

    I have done this code:

    if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER)        {
        myButton.fireEvent( new GwtEvent<ClickHandler>() {
            @Override
            public com.google.gwt.event.shared.GwtEvent.Type<ClickHandler> getAssociatedType() {
            return ClickEvent.getType();
            }
            @Override
            protected void dispatch(ClickHandler handler) {
                handler.onClick(null);
            }
       });
    }
    

    Of course myButton must be final or public cause you are inside another event handler.

    0 讨论(0)
  • 2020-12-03 10:50

    Maybe ClickListenerCollection.fireClick() does what you want. I haven't used it though

    Sorry, that is deprecated and maybe not even useful. I guess you shoul look at widget.delegateEvent instead.

    0 讨论(0)
  • 2020-12-03 10:53

    The method described by DLH should work and except for the detail argument (which I have no idea what's it for) you have the other arguments available in the KeyPressEvent.

    Another possible solution is to call the native JavaScript click() on the element. I've done this in a Button widget (Which is available as open source). See click() method in the following class: http://code.google.com/p/cobogw/source/browse/trunk/widgets/src/main/java/org/cobogw/gwt/user/client/ui/Button.java), which calls a specific Event2 class, that implements the browser specific versions of the click method.

    To use this method, you could simply add the jar file provided with cobogw to your project, include the Event.gwt.xml and call Event2.fireClickEvent(getElement()); in your method or only use the code from the classes Event2 and Event in your own project

    This solution also allows you the programmatically fire a click event.

    Also take a look at the onBrowserEvent implementation in the Button class mentioned above , since it handles the key event in a similar way you want, and works around the problem of the firing of multiple key events, when you only want to generate 1 click event.

    0 讨论(0)
  • 2020-12-03 10:59

    You can also use a simple JSNI method to do it. Just pass your element [e.g. button.getElement()] to this method:

    public static native void clickElement(Element elem) /*-{
        elem.click();
    }-*/;
    
    0 讨论(0)
  • 2020-12-03 11:00

    If the target is a button you can just call its click() method. Otherwise you can do something like this:

    private void click(HasHandlers handlerSource) {
      NativeEvent event = Document.get().createClickEvent(0, 0, 0, 0, 0, false, false, false, false);
      DomEvent.fireNativeEvent(event, handlerSource);
    }
    
    0 讨论(0)
  • 2020-12-03 11:04

    You can just do this:

    focusPanel.fireEvent(new ClickEvent(){});
    
    0 讨论(0)
提交回复
热议问题