jldupont
jldupont

Reputation: 96716

GWT: How do I mouseUp events?

In GWT, how do I get the "mouseUp" events when dragging an element?

I'd like to get the following flow:

I can get "mouseDown" and "mouseUp" when I do not move the cursor but the instant I move the cursor, I don't get "mouseUp" events anymore.

I've tried to put event.preventDefault(); in both "mouseDown" and "mouseMove" handlers to no avail.

Please help.

Upvotes: 2

Views: 3157

Answers (3)

Stefan
Stefan

Reputation: 14863

of course the MouseUp handler only works as long as you are on the widtet. That's why you CAN'T use it for drag and drop, because as soon as you move out of the widget the it doesn't react to the MouseUp event.

Here is a simple example:

package XXX.client.XXXXXX;

import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;

public class MouseUp {
    public MouseUp(){
        Label label = new Label("hallo");
        label.getElement().getStyle().setProperty("backgroundColor","red");
        RootPanel.get().add(label);

        label.addMouseDownHandler(new MouseDownHandler() {

            @Override
            public void onMouseDown(MouseDownEvent event) {
                Label label = new Label("onMouseDown");
                RootPanel.get().add(label);             
            }
        });
        label.addMouseUpHandler(new MouseUpHandler() {

            @Override
            public void onMouseUp(MouseUpEvent event) {
                Label label = new Label("onMouseUp");
                RootPanel.get().add(label);                     
            }
        });
    }
}

If you want to use drag and drop functionallity I recomend looking at

Upvotes: 1

Thomas Broyer
Thomas Broyer

Reputation: 64541

For DnD you generally need to setCapture. And don't forget to releaseCapture when you stop dragging (e.g. mouse up and/or escape key).

Have a look at the code of com.google.gwt.user.client.ui.DialogBox, which does just that.

Upvotes: 5

Andrei Volgin
Andrei Volgin

Reputation: 41089

Try attaching a MouseUp handler inside the MouseDown event (i.e. in onMouseDown()).

Upvotes: 0

Related Questions