user1899956
user1899956

Reputation: 23

SmartGWT: Handling mouse down when a child element of Canvas is clicked

I have smartgwt Canvas to which a TextArea is added. I want to handle MouseDown event and change its border when user clicks anywhere on the canvas. My code looks like this.

final com.smartgwt.client.widgets.Canvas can = new com.smartgwt.client.widgets.Canvas();
can.setCanFocus(true);
can.setBorder("2px Solid Blue");
can.setCanDragResize(true);
TextArea ta = new TextArea();
can.addChild(ta);
can.addMouseDownHandler(new MouseDownHandler() {

            @Override
            public void onMouseDown(MouseDownEvent event) {

                ChangeCanvasBorder(can);

            }
        });

MouseDown event is not fired when i click on TextArea contained in Canvas. Is there a way to handle click on canvas child elements?

I am new to gwt. I might have missed some configuration.

As an alternative i have tried the gwt's FocusPanel and i am able receive MouseDown when the TextArea added to FocusPanel is clicked.

I am using smartgwt 2.4 and gwt 2.4.

Thank you in advance for help.

Upvotes: 1

Views: 1428

Answers (1)

mikereem
mikereem

Reputation: 235

As ZalewaPL said, it is better to avoid mixing gwt and smartgwt widgets. I suggest you to use the TextAreaItem from smartgwt added to a DynamicForm. Here is a simple example:

final Canvas can = new Canvas();
can.setCanFocus(true);
can.setBorder("2px Solid Blue");
can.setCanDragResize(true);
DynamicForm form = new DynamicForm();
TextAreaItem tai = new TextAreaItem("textarea");
form.setFields(tai);
can.addChild(form);
can.addMouseDownHandler(new MouseDownHandler() {

    @Override
    public void onMouseDown(MouseDownEvent event) {
        changeCanvasBorder(can);
    }
});

But if you still want to use the TextArea widget from gwt, then you can do this:

abstract class CustomMouseDownHandler implements com.smartgwt.client.widgets.events.MouseDownHandler, com.google.gwt.event.dom.client.MouseDownHandler {
    @Override
    public void onMouseDown(com.smartgwt.client.widgets.events.MouseDownEvent event) {
        onMouseDown();
    }

    @Override
    public void onMouseDown(com.google.gwt.event.dom.client.MouseDownEvent event) {
        onMouseDown();
    }

    public abstract void onMouseDown();
}

final Canvas can = new Canvas();
can.setCanFocus(true);
can.setBorder("2px Solid Blue");
can.setCanDragResize(true);
TextArea ta = new TextArea();
CustomMouseDownHandler mdh = new CustomMouseDownHandler() {

    @Override
    public void onMouseDown() {
        changeCanvasBorder(can);
    }
};
ta.addMouseDownHandler(mdh);
can.addChild(ta);
can.addMouseDownHandler(mdh);

This one creates a new handler abstract class, which implements both MouseDownHandler interfaces and forwards their onMouseDown method to a new onMouseDown method. You have to create one implementation for this abstract class and use it for the Canvas and the TextArea widget too.

Upvotes: 1

Related Questions