How to create an inner popup in JavaFX styled with

2019-05-30 09:16发布

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.

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?

2条回答
We Are One
2楼-- · 2019-05-30 09:56

ControlsFX has a PopOver control you might like. That PopOver can use any Node for its content, so you can simply create a popover, load a node from FXML and set the content of the popover to that node.

查看更多
神经病院院长
3楼-- · 2019-05-30 09:59

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());

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

查看更多
登录 后发表回答