weijenli
weijenli

Reputation: 75

GWT Custom right click on a widget

I need to implement widget that I can right click to disable it and right click to enable it again. My code is like this:

widget.sinkEvents(Event.ONCONTEXTMENU);
widget.addHandler(new ContextMenuHandler() {
    @Override
    public void onContextMenu(ContextMenuEvent event)
    {
        event.preventDefault();
        event.stopPropagation();
        if (widget.isEnabled()) widget.setEnabled(false);
        else widget.setEnabled(true);
    }
}, ContextMenuEvent.getType());

This works when the widget is enabled. Meaning I can right click to disable this widget. However, when the widget is disabled, it does not trigger the right click event.

Is there a way to make my custom right click working on a disabled widget?

Thanks,

Upvotes: 1

Views: 502

Answers (1)

tangent
tangent

Reputation: 388

If the event doesn't fire on the disabled widget, you could try one of the following workarounds:

  • After disabling the widget, create a Label with opacity of 0 with the same dimensions as the widget, and place it over the widget with a ContextMenuHandler that re-enables the widget and removes itself.
  • Instead of disabling the widget, replace it with an image of a disabled widget. Bit of a hack, but might work if this is a one-off
  • After disabling the widget, add a different event handler to it to re-enable it (you could try MouseDown, MouseOver, Click).

Upvotes: 1

Related Questions