JavaFX:一行填充了一个条目后的ContextMenu? [英] JavaFX: ContextMenu after a row has been filled with an entry?
问题描述
我正在使用此链接为每个表格行创建一个ContextMenu。现在我遇到了问题因为我不确定在'type'插入行之后如何附加ContextMenu。
I am using this link for creating a ContextMenu for each table row. Right now I'm running into problems because I'm not sure how to attach a ContextMenu after the 'type' has been inserted into a row.
让我说'我使用.zip编辑器程序,它列出了内容。我有一个Image,一个文本文件和其他一些东西,所有这些东西都在一个名为 Entry 的类下面。我的表的泛型类型是'Entry',我希望能够根据它的底层子类类型为每个条目创建一个上下文菜单(就像ImageEntry可能返回一个菜单项在图像编辑器中打开它)。 .etc)。
Lets say I'm using a .zip editor program, and it lists the contents. I have an Image, and a text file, and some other stuff, all of them are under a class called Entry. My table's generic type is 'Entry', and I'd like to be able to create a context menu for each entry based on it's underlying subclass type (like an ImageEntry might return a menu item to open it up in an image editor...etc).
现在我有一个通用的上下文菜单,但是显示一个关于用图像编辑器打开文本文件的菜单项并不是很好...
Right now I have a generic context menu for everything, but it's not great displaying a menu item about opening a text file with an image editor...
这可能吗?如果是这样,做什么的正确方法是什么?
Is this possible to do? If so, what is the proper way to go about doing it?
推荐答案
在行的 itemProperty
(表示行中显示的项目)并在更改时更新上下文菜单:
Add a listener to the row's itemProperty
(which represents the item displayed in the row) and update the context menu when it changes:
table.setRowFactory(new Callback<TableView<Person>, TableRow<Person>>() {
@Override
public TableRow<Person> call(TableView<Person> tableView) {
final TableRow<Person> row = new TableRow<>();
final ContextMenu contextMenu = new ContextMenu();
row.itemProperty().addListener((obs, oldPerson, newPerson) -> {
contextMenu.getItems().clear();
// add items to context menu depending on value of newPerson
// ...
});
// Set context menu on row, but use a binding to make it only show for non-empty rows:
row.contextMenuProperty().bind(
Bindings.when(row.emptyProperty())
.then((ContextMenu)null)
.otherwise(contextMenu)
);
return row ;
}
});
这篇关于JavaFX:一行填充了一个条目后的ContextMenu?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!