如何在使用FXML设计的JavaFX中创建内部弹出窗口

问题描述:

我有一个FXML文件,我用它来允许用户在请求时输入。现在我只是将它放在一个新的阶段并执行 Stage.show()。我希望它不会出现在一个新窗口中,表现得更像 ContextMenu

I have an FXML file that I'm using to allow user input when requested. Right now I just put it in a new stage and do Stage.show(). I would like to not have it appear in a new window and behave more like a ContextMenu.

看着 ContextMenu 类似乎我不能根据FXML文件设置内容。有没有办法用 ContextMenu Popup 或其他一些我不知道的类?

Looking at ContextMenu class it doesn't appear that I can set the content based off an FXML file. Is there a way to do this either with ContextMenu or Popup or some other class I am unaware of?

虽然该库非常好,但我想要一些简单的东西,不需要第三方下载。我想出了这个:

Although that library is quite nice, I wanted something simple that didn't require 3rd party downloads. I came up with this:

Popup popup = new Popup();
CustomController controller = new CustomController();
FXMLLoader loader = new FXMLLoader(getClass().getResource(fxmlfile));
loader.setController(controller);
popup.getContent().add((Parent)loader.load());

问题是我没有意识到父$ c对于方法,$ c>可被视为节点 Popup#getContent #add

The problem was I didn't realize that a Parent could be considered a Node for the method Popup#getContent#add