4

This is probably a really simple thing, but I don't know how to implement the following.

package mods.client.resultSelector;

import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;

public class MousyAbsolutePanel extends Composite implements MouseDownHandler {

    AbsolutePanel abs = new AbsolutePanel();

    public MousyAbsolutePanel(int width){
        System.out.println("MousyAbsolutePanel being created with width:" + width);
        initWidget(abs);
        abs.setWidth(String.valueOf(width));
        abs.setHeight("100%");
        abs.setStyleName("mousyAbsolutePanel");
    }

    public void onMouseDown(MouseDownEvent event) {
        System.out.println("onMouseDown()");
    }

}

I want to have what is effectively a absolutePanel that can accept mouse events. However, within the Composite object I don't know how to tie the the handler I have written (the onMouseDown() thing) with the abs variable. To put it succinctly, I want the abs AbsolutePanel to respond when it is clicked upon, but AbsolutePanels do not naturally accept click events. How do I go about doing this?

Apologies in advance if this is stupid simple, but I just don't quite know how to implement this behavior, and I have not seen it mentioned in the searches I have done.

Stephen Cagle
  • 14,124
  • 16
  • 55
  • 86

4 Answers4

5

In this case, extending the Composite is probably not the best idea unless you're deliberately blocking access to the AbsolutePanel you create in the constructor. If you extend the AbsolutePanel you'll be able to reuse the code to add/remove widgets etc..

Here's how I'd recommend you do it:

package mods.client.resultSelector;

import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.HasMouseDownHandlers;
import com.google.gwt.user.client.ui.AbsolutePanel;


public class MousyAbsolutePanel extends AbsolutePanel implements
                         MouseDownHandler,HasMouseDownHandlers {

    public MousyAbsolutePanel(int width) {
        System.out.println("MousyAbsolutePanel being created with width:" + width);

        this.setWidth(String.valueOf(width));
        this.setHeight("100%");
        this.setStyleName("mousyAbsolutePanel");

        this.addMouseDownHandler(this);
    }

    /** 
     * MouseDownHandler
     */
    public void onMouseDown(MouseDownEvent event) {
        System.out.println("onMouseDown()");
    }

    /**
     * HasMouseDownHandlers - Code to add handlers to the panel
     */  
    public HandlerRegistration addMouseDownHandler(MouseDownHandler handler) {
         return addDomHandler(handler, MouseDownEvent.getType());
    }

}

Then you can access MousyAbsolutePanel the same way you access an AbsolutePanel, but with the additional event handlers, style etc..

hannson
  • 4,465
  • 8
  • 38
  • 46
1

Here is another post which might be helpful:

Adding ClickHandler to div which contains many other widget

Calling sinkEvents(Event.ONCLICK) and then adding a ClickHandler using only addHandler worked great for me.

Community
  • 1
  • 1
Andrew
  • 11
  • 1
0

The best thing to do for learning how/what to do is to look at an example. E.g., the FocusWidget class (which implements HasClickHandlers).

Specifically, look at the addClickHandler() method, and trace it through.

jgindin
  • 948
  • 8
  • 15
0

Once you call initWidget(Widget), Composite will correctly proxy the event registration. So, you can just call MousyAbsolutePanel's inherited addDomHandler(...).

Fiona Hopkins
  • 1,966
  • 15
  • 14