Blocking / Disabling JavaFX 2 WebView

I am 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 be displayed on a right click. I'm new to technology and can't seem to find a way to do this.


source to share

7 replies

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



I came up with a working, but ugly, inelegant and, I would say, guerrilla solution, which I don't really like, but I don't really have (or just can't find) any other way out.

It includes a modification to the EventDispatcher WebView.

So my EventDispatcher implementation needs a reference to the original WebView EventDispatcher and looks like this:

public class MyEventDispatcher implements EventDispatcher {

private EventDispatcher originalDispatcher;

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

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


Each event is dispatched through our dispatcher and I check if it clicked correctly. If this I just destroy and continue on.

To do this, you must use a WebView like this:

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


Every comment, hint, etc. are evaluated.



You can flatten the context menus using the following css.

.context-menu     { -fx-background-color: transparent; }
.menu-item        { -fx-background-color: transparent; }
.menu-item .label { -fx-text-fill: transparent; }
.menu-item:show-mnemonics .mnemonic-underline { -fx-stroke: -transparent; }


This will make all context menus and menu items transparent. I am not sure about the css selector you can use so this only applies to WebView context menus. If you don't have any other menus in your app, it might not matter.



You can do it with js ( jquery


$(document).ready( function() { document.oncontextmenu = function() { return false } } );




Unfortunately, this is not yet possible. There is a feature request for this that you can track:



As of JavaFX 2.2+, it is now possible to set WebView's ContextMenuEnabled to false:



WebView API Document



This will remove the context menu for the entire step:

            new EventHandler<MouseEvent>() {
    public void handle(MouseEvent event) {
        if (event.getButton() == MouseButton.SECONDARY) {





All Articles