Blocking/disabling JavaFX 2 WebView right click


Question

I'm looking for a way to block/disable right click in javafx.scene.web.WebView. To be more specific I don't want the context menu to show up on right click. I'm new to the technology and can't find the way to do it.

1
4
3/28/2012 3:22:51 PM

Accepted Answer

Just for the record, it is implemented in JavaFX 2.2. See documentation for setContextMenuEnabled

13
10/18/2012 12:47:57 PM

I've came up with working, but ugly, inelegant and, I'd say, partisan solution, which I don't really like, but actually I have no (or just can't find) other way out.

It includes modifying EventDispatcher of WebView.

So my implementation of EventDispatcher needs a reference to original WebView EventDispatcher and looks like that:

public class MyEventDispatcher implements EventDispatcher {

private EventDispatcher originalDispatcher;

public MyEventDispatcher(EventDispatcher originalDispatcher) {
    this.originalDispatcher = originalDispatcher;
}

@Override
public Event dispatchEvent(Event event, EventDispatchChain tail) {
    if (event instanceof MouseEvent) {
        MouseEvent mouseEvent = (MouseEvent) event;
        if (MouseButton.SECONDARY == mouseEvent.getButton()) {
            mouseEvent.consume();
        }
    }
    return originalDispatcher.dispatchEvent(event, tail);
}
}

Everytime event is dispatched it goes through our dispatcher and I check if it's right click. If it is I just consume it and proceed further.

To make it work you have to use WebView like that:

WebView webView = new WebView();
EventDispatcher originalDispatcher = webView.getEventDispatcher();
webView.setEventDispatcher(new MyEventDispatcher(originalDispatcher));

Every comment, clue and so on are appreciated.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon