JavaFX MenuItem对MouseEvent没有反应. [英] JavaFX MenuItem does not react on MouseEvent.CLICKED

查看:64
本文介绍了JavaFX MenuItem对MouseEvent没有反应.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Oracle示例,我正在使用TreeView编写一个小的桌面应用程序: https://docs.oracle.com/javafx/2/ui_controls/tree-view.htm .

I am writting a little desktop application with a TreeView according to the Oracle-Example from here: https://docs.oracle.com/javafx/2/ui_controls/tree-view.htm.

我想从ContextMenu的MenuItem动作中触发一个事件,该事件将在我从中打开ContextMenu的项目下方创建一个新的TreeItem.

From a MenuItem action of a ContextMenu, I would like to fire an event which shall create a new TreeItem below the item where I opened the ContextMenu from.

对于MenuItem,可以使用 setOnAction(EventHandler< ActionEvent>事件)方法,但是我只想通过鼠标左键触发操作.

For MenuItem, it is possible to use the setOnAction(EventHandler<ActionEvent> event) method, but I only want to fire the action from a left mouse-click.

首先,无法将EventHandler添加到MenuItem中,尽管它提供了事件类型MouseEvent.ANY(或其他任何东西)的方法 addEventHandler(EventType type,EventHandler< EventType> handler)别的).不会调用事件处理程序的处理方法.

First, it is not possible to add an EventHandler to a MenuItem although it provides the method addEventHandler(EventType type, EventHandler<EventType> handler) with the event-type MouseEvent.ANY (or anything else). The handle-method of the event-handler is not called.

第二,我可以使用工作区,方法是通过 menuItem.setGraphic(label)将Label添加到MenuItem并将EventHandler添加到标签.尽管 MouseEvent.MOUSE_CLICKED 未由Label上的EventHandler的句柄方法调用,该方法仍然有效.

Second, i can use a workarround by adding a Label to a MenuItem by menuItem.setGraphic(label) and add an EventHandler to the label. This one works although MouseEvent.MOUSE_CLICKED is not called by an EventHandler's handle-method on a Label.

这是正常"行为吗?我知道标签不会对点击事件做出反应,但是我不明白为什么无法在MenuItem上注册单独的EventHandler或EventFilter.

Is this "normal" behaviour? I understand that a label does not react on a click-event, but I do not understand why it is not possible to register a separate EventHandler or EventFilter on a MenuItem.

推荐答案

ContextMenu 使用 MenuItemContainer ,即

负责在菜单中布置一行的容器-在其他容器中的话,它包含并布置了一个MenuItem,无论它是特定的亚型.

Container responsible for laying out a single row in the menu - in other words, this contains and lays out a single MenuItem, regardless of it's specific subtype.

为此,似乎创建了代表 MenuItem 的新 Nodes .因此,添加到 MenuItem 的任何 EventHandlers 都不会被调用.

Fur this purpose it seems to create new Nodes representing the MenuItem. So any EventHandlers added to the MenuItem will not be called.

要使其按预期工作,可以使用 CustomMenuItem 并将相应的 EventHandler 添加到其 content 中:

To make it work as you intended, you can use a CustomMenuItem and add the according EventHandler to its content:

 public class ContextMenuCell extends TreeCell<String> {

        private ContextMenu menu;

        public ContextMenuCell() {
            Label lbl = new Label("Add item");
            MenuItem menuItem = new CustomMenuItem(lbl);

            lbl.setOnMouseClicked(evt -> {
                if (evt.getButton() != MouseButton.PRIMARY) {
                    return;
                }

                TreeItem treeItem =
                        new TreeItem<String>("New item");

                if (getTreeItem().isLeaf()) {
                    getTreeItem().getParent().getChildren().add(getIndex(), treeItem);
                } else {
                    getTreeItem().getChildren().add(0, treeItem);
                }

            });

            menu = new ContextMenu(menuItem);
        }

        @Override
        public void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);

            if (empty) {
                setText(null);
                setGraphic(null);
            } else {
                setText(item);
                setGraphic(getTreeItem().getGraphic());
                setContextMenu(menu);
            }
        }
    }

这篇关于JavaFX MenuItem对MouseEvent没有反应.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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