Adding a MouseOverHandler to an Element?

前端 未结 4 2047
悲&欢浪女
悲&欢浪女 2021-02-08 18:14

I would like to listen for the mouse over event in GWT 1.6. Since GWT 1.6 has introduced handlers and deprecated listeners I\'m unsure as to how I can accomplish this with what

4条回答
  •  说谎
    说谎 (楼主)
    2021-02-08 18:48

    You'd want to implement these interfaces in your class:

    • HasMouseOverHandlers
    • HasMouseOutHandlers
    • MouseOverHandler
    • MouseOutHandler

    MouseOverEvent is fired when the mouse enters the element, and MouseOutEvent is fired when it's no longer over.

    HasMouseOverHandler is implemented like this:

    public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
        return addDomHandler(handler, MouseOverEvent.getType());
    }
    

    HasMouseOutHandler is implemented like this:

    public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
        return addDomHandler(handler, MouseOutEvent.getType());
    }
    

    After that you just handle the events with a MouseOverHandler and MouseOutHandler, should be pretty straightforward after that.

    If you want to add an EventHandler to an Element that already exists in the HTML the only idea I've come up with is creating a wrapper class. This is completely untested.

    class ElementWrapper extends UIObject implements HasMouseOverHandlers, 
    HasMouseOutHandlers
    {
         public ElementWrapper(Element theElement)
         {
            setElement(theElement);
         }   
    
        public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
            return addDomHandler(handler, MouseOutEvent.getType());
        }
    
        public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
            return addDomHandler(handler, MouseOverEvent.getType());
        }
    }
    

    Then you could get an existing Element from the HTML and initialize like this:

    onModuleLoad()
    {
        Element theElement = RootPanel().get("elementID");
        ElementWrapper wrapper = new ElementWrapper(theElement);
        wrapper.addMouseOverHandler(new myHandler());
    
    }
    

    Hope this helps.

提交回复
热议问题