How to add MouseHandler in constructor? (NPE)

17 views
Skip to first unread message

membersound

unread,
Dec 28, 2012, 9:17:01 AM12/28/12
to google-we...@googlegroups.com
I know I can just bind any ui component to any events by using @UiHandler like this:

<g:FocusPanel ui:field="panel">

@UiField
FocusPanel panel;

@UiHandler("panel")
void handleClick(MouseDownEvent event) {
    //...
}

So far so good, but how can I "manually" add a mouse handler within the class constructor:
            panel.addMouseDownHandler(new MouseDownHandler() { //NPE
                @Override
                public void onMouseDown(MouseDownEvent event) {
                    Window.alert("mouse down");
                }
            });

If I write this in the constructor of the java class, I'm getting NullPointerException at this stage where the handler is added to the panel.
What am I missing here?

Jens

unread,
Dec 28, 2012, 10:39:56 AM12/28/12
to google-we...@googlegroups.com
In your constructor you have to do it after 

initWidget(uiBinder.createAndBindUi(this));

All your @UiField variables are filled by the call to uiBinder.createAndBindUi(this).

Also if you use @UiField(provided = true) then you have to instantiate the field yourself before uiBinder.createAndBindUi(this) is called.

-- J.
Reply all
Reply to author
Forward
0 new messages