How to create an inner popup in JavaFX styled with FXML

女生的网名这么多〃 提交于 2019-12-08 05:34: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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/30448611/how-to-create-an-inner-popup-in-javafx-styled-with-fxml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!