Java 一行 TableView 上的上下文菜单?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21009377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Context Menu on a row of TableView?
提问by Michael Scott
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.
我正在使用 JavaFX 并且我的应用程序有一个表,我可以向表中添加元素,但我想创建一个上下文菜单,当我右键单击该行时显示在该行上。
What I have...
我拥有的...
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 is the context menu with menu items. connectedUsers is the TableView
contextMenu 是带有菜单项的上下文菜单。connectedUsers 是 TableView
The following is the closest I can get, but this shows the context menu at the bottom of the TableView
以下是我能得到的最接近的,但这显示了 TableView 底部的上下文菜单
contextMenu.show(connectedUsers, Side.BOTTOM, 0, 0);
采纳答案by Anshul Parashar
try this..
尝试这个..
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());
}
}
});
回答by Luca S.
I believe that the best solution would be this as discussed in here.
我相信最好的解决方案是这里讨论的这个。
table.setRowFactory(
new Callback<TableView<Person>, TableRow<Person>>() {
@Override
public TableRow<Person> call(TableView<Person> tableView) {
final TableRow<Person> row = new TableRow<>();
final ContextMenu rowMenu = new ContextMenu();
MenuItem editItem = new MenuItem("Edit");
editItem.setOnAction(...);
MenuItem removeItem = new MenuItem("Delete");
removeItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
table.getItems().remove(row.getItem());
}
});
rowMenu.getItems().addAll(editItem, removeItem);
// only display context menu for non-empty rows:
row.contextMenuProperty().bind(
Bindings.when(row.emptyProperty())
.then(rowMenu)
.otherwise((ContextMenu)null));
return row;
}
});
回答by ceklock
JavaFX 8 (with Lambda):
JavaFX 8(使用 Lambda):
MenuItem mi1 = new MenuItem("Menu item 1");
mi1.setOnAction((ActionEvent event) -> {
System.out.println("Menu item 1");
Object item = table.getSelectionModel().getSelectedItem();
System.out.println("Selected item: " + item);
});
ContextMenu menu = new ContextMenu();
menu.getItems().add(mi1);
table.setContextMenu(menu);
See also: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ContextMenu.html
另见:https: //docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ContextMenu.html