修改JavaFX WebView中的上下文菜单 [英] Modify context menu in JavaFX WebView

查看:441
本文介绍了修改JavaFX WebView中的上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在用户右键单击链接时向JavaFX WebView中的上下文菜单添加一些选项,但是我无法弄清楚如何执行此操作。

I'm trying to add some options to the context menu in a JavaFX WebView when the user right clicks a link, however I can't figure out how to do it.

我发现我可以使用以下方式添加自己的上下文菜单:

I found I could add my own context menu using:

            final ContextMenu contextMenu = new ContextMenu();
            view.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent mouseEvent) {
                    System.out.println(mouseEvent.getEventType());
                    if(mouseEvent.isSecondaryButtonDown()) {
                        System.out.println("Secondary");
                        contextMenu.show(view, mouseEvent.getSceneX(), mouseEvent.getSceneY());
                    } else if (mouseEvent.isPrimaryButtonDown()) {
                        System.out.println("Primary");
                    } else if (mouseEvent.isMiddleButtonDown()) {
                        System.out.println("Middle");
                    }
                }
            });

            javafx.scene.control.MenuItem menuItem1 = new javafx.scene.control.MenuItem("View Source");
            menuItem1.setOnAction(new EventHandler<javafx.event.ActionEvent>() {
                @Override
                public void handle(javafx.event.ActionEvent actionEvent) {
                    System.out.println("Link: " + rightClickURL);
                    System.out.println("You clicked view source");
                }
            });

            contextMenu.getItems().add(menuItem1);

不幸的是,当我这样做时,两个菜单都显示出来:

Unfortunately, when I do that both menus appear:

如果我使用 view.setContextMenuEnabled(false); 然后默认菜单消失。不幸的是,这样做也阻止我检测哪个链接被右击。以下是我正在使用的代码:

If I use view.setContextMenuEnabled(false); then the default menu disappears. Unfortunately doing that also prevents me from detecting which link was right clicked. Here is the code I'm using for that:

            engine.getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
                @Override
                public void changed(ObservableValue ov, Worker.State oldState, Worker.State newState) {
                    if (newState == Worker.State.SUCCEEDED) {
                        EventListener listener = new EventListener() {

                            @Override
                            public void handleEvent(org.w3c.dom.events.Event evt) {
                                String domEventType = evt.getType();
                                if (domEventType.equals(EVENT_TYPE_CLICK)) {
                                    String href = ((Element)evt.getTarget()).getAttribute("href");

                                } else if (domEventType.equals(EVENT_TYPE_RIGHT_CLICK)) {
                                    String href = ((Element)evt.getTarget()).getAttribute("href");
                                    rightClickURL = href;
                                    System.out.println(href);
                                }
                            }
                        };

                        Document doc = engine.getDocument();
                        NodeList nodeList = doc.getElementsByTagName("a");
                        for (int i = 0; i < nodeList.getLength(); i++) {
                            ((EventTarget) nodeList.item(i)).addEventListener(EVENT_TYPE_CLICK, listener, false);
                            ((EventTarget) nodeList.item(i)).addEventListener(EVENT_TYPE_RIGHT_CLICK, listener, false);
                        }
                    }
                }
            });

所以我的问题是:如何访问默认的ContextMenu以便我可以自定义它?我已经搜索过文档,但找不到任何允许您访问默认ContextMenu的方法。似乎必须有一种方法可以做到这一点,但我很难过如何。

So my question is this: how can I access the default ContextMenu so I can customize it? I've scoured the docs but cannot find any method that allows you to access the default ContextMenu. It seems like there must be a way to do this but I'm stumped as to how.

如果无法自定义默认的ContextMenu,有谁知道我怎么做右键单击链接时显示自定义上下文菜单,还捕获单击了哪个链接?我一直试图在没有运气的情况下实现这一天......

If customizing the default ContextMenu is not possible, does anyone know how I can show a custom context menu when right clicking a link and also capture which link was clicked? I've been trying to achieve this for days with absolutely no luck...

推荐答案

目前这是不可能的。 JavaFX上有一个未解决的问题:
https://javafx-jira.kenai.com/浏览/ RT-20306

Currently that's not possible. There is an open issue on JavaFX for that: https://javafx-jira.kenai.com/browse/RT-20306

如果您只想添加不同的上下文菜单,那么您可以这样做

If you just want to add a different context menu, then you could do that

 webView.setOnMouseClicked(new EventHandler<MouseEvent>() {

                @Override
                public void handle(MouseEvent mouse) {
                    if (mouse.getButton() == MouseButton.SECONDARY) {
                        menu = new ContextMenu();
                       //add some menu items here
                       menu.show(this, mouse.getScreenX(), mouse.getScreenY());
                    } else {
                        if (menu != null) {
                            menu.hide();
                        }
                    }
                }
            });

这篇关于修改JavaFX WebView中的上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆