[英]How to get the parent MenuBar of a JavaFX Menu
I have not been able to find API in the JavaFX Menu
class that would allow me to retrieve the parent MenuBar
. 我无法在JavaFX
Menu
类中找到允许我检索父MenuBar
API。 Any hints, including the usage of internal API are very welcome. 任何提示,包括内部API的使用都是非常受欢迎的。 (JavaFX 8)
(JavaFX 8)
Background: Usually any JavaFX Node
can give you it's parent with the method getParent()
. 背景:通常任何JavaFX
Node
都可以使用方法getParent()
其父级提供。 But since Menu
and MenuItem
do not inherit from Node
, this possibility is not available. 但由于
Menu
和MenuItem
不从Node
继承,因此无法使用此可能性。 The MenuItem
class (and therefore also the Menu
class) has two similar methods: MenuItem
类(以及Menu
类)有两个类似的方法:
getParentPopup()
to get a parent ContentMenu
getParentPopup()
获取父ContentMenu
getParentMenu()
to get a parent Menu
getParentMenu()
获取父Menu
So I would have expected something like getParentMenuBar()
, but it's not there. 所以我会期待像
getParentMenuBar()
这样的东西,但它并不存在。
EDIT: I just found the jira feature request for this API extension: https://bugs.openjdk.java.net/browse/JDK-8091154 编辑:我刚刚找到此API扩展的jira功能请求: https : //bugs.openjdk.java.net/browse/JDK-8091154
Has anybody found a workaround for this? 有人为此找到了解决方法吗?
I'd question why you need access going from Menu back to MenuBar, what problem are you trying to solve? 我问你为什么需要从Menu返回到MenuBar的访问权限,你试图解决什么问题?
One possible approach is to use the arbitrary properties map on Node to store a reference from Menu to MenuBar as this example shows. 一种可能的方法是使用Node上的任意属性映射来存储从Menu到MenuBar的引用,如此示例所示。
public class MenuToMenuBar extends Application {
public static void main(String args[]) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
MenuBar bar = new MenuBar();
stage.setScene(new Scene(bar));
Menu menu = new Menu("Foo");
MenuItem menuItem = new MenuItem("Baz");
menu.getItems().add(menuItem);
bar.getMenus().add(menu);
// put a reference back to MenuBar in each Menu
for (Menu each : bar.getMenus()) {
each.getProperties().put(MenuBar.class.getCanonicalName(), bar);
}
menuItem.setOnAction((e) -> {
// retrieve the MenuBar reference later...
System.out.println(menuItem.getParentMenu().getProperties().get(MenuBar.class.getCanonicalName()));
});
stage.show();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.