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.

+3


source to share


7 replies


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



+12


source


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;
}

@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);
}
}

      



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.

+5


source


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.

+3


source


You can do it with js ( jquery

)

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

      

+3


source


Unfortunately, this is not yet possible. There is a feature request for this that you can track: http://javafx-jira.kenai.com/browse/RT-15684

+2


source


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

webView.setContextMenuEnabled(false);

      

WebView API Document

+2


source


This will remove the context menu for the entire step:

primaryStage.getScene().addEventFilter(MouseEvent.MOUSE_RELEASED, 
            new EventHandler<MouseEvent>() {
    public void handle(MouseEvent event) {
        if (event.getButton() == MouseButton.SECONDARY) {
            event.consume();
        }

    }
});

      

+1


source







All Articles