一行TableView上的上下文菜单? [英] Context Menu on a row of TableView?

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

问题描述

我正在使用JavaFX,我的应用程序有一个表,可以向表中添加元素,但是我想创建一个右键单击该行时显示在该行上的上下文菜单。

I am using JavaFX and my application has a table and I can add elements to the table but I want to create a context menu that displays on a row when I right click on that row.

我所拥有的...

在Scene Builder中,我有一个方法在激活上下文菜单时运行,但这并不完全我想要的是。这真的很好,因为我可以随时通过编程方式从表中获取选定的项目。如果我保留当前的状态,那么问题是使上下文菜单在所选元素上弹出。

In Scene Builder I have a method that runs on when the Context Menu is activated but that isn't exactly what I want. This would be fine really because I am programmatically grab the selected item from the table whenever I want. The issue, if I keep what I currently have, is getting the context menu to popup at the selected element.

contextMenu是包含菜单项的上下文菜单。
connectedUsers是TableView

contextMenu is the context menu with menu items. connectedUsers is the TableView

以下是我能得到的最接近的内容,但这显示了TableView底部的上下文菜单

The following is the closest I can get, but this shows the context menu at the bottom of the TableView

contextMenu.show(connectedUsers, Side.BOTTOM, 0, 0);


推荐答案

尝试一下。.

ContextMenu cm = new ContextMenu();
MenuItem mi1 = new MenuItem("Menu 1");
cm.getItems().add(mi1);
MenuItem mi2 = new MenuItem("Menu 2");
cm.getItems().add(mi2);

table.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {

    @Override
    public void handle(MouseEvent t) {
        if(t.getButton() == MouseButton.SECONDARY) {
            cm.show(table, t.getScreenX(), t.getScreenY());
        }
    }
});

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

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