[英]JavaFX - how to center JFXpopup on stage and dynamic size of popup
I want to make a JFXpopup at the center of the screen with a dynamic width & height. 我想在屏幕中央制作一个具有动态宽度和高度的JFXpopup。 This is what I currently have: 这是我目前拥有的:
This is the application's first look. 这是应用程序的第一眼。 To open the popup I have to click an item in the contextmenu: 要打开弹出窗口,我必须单击上下文菜单中的一个项目:
and after that you get to see the popup: 之后,您会看到弹出窗口:
The popup is centered on the x axis, but apparently not on the y axis. 弹出窗口位于x轴中心,但显然不在y轴上。 Also when I resize my stage and reopen the popup the size doesn't change. 另外,当我调整舞台大小并重新打开弹出窗口时,大小不会改变。
Now for some code: 现在获取一些代码:
contextMenu.getNewLeaf().setOnAction(event -> {
popup.setPopupContent(createRegistrationFormPane());
popup.setAutoFix(true);
double width = globals.getPrimaryStage().getWidth() / 1.5;
double height = globals.getPrimaryStage().getHeight() / 1.5;
System.out.println("stage - width: " + globals.getPrimaryStage().getWidth() + "height: " + globals.getPrimaryStage().getHeight());
System.out.println("width: " + width + " height: " + height);
popup.getPopupContent().setPrefWidth(width);
popup.getPopupContent().setPrefHeight(height);
double anchorX = (globals.getPrimaryStage().getWidth() - width) / 2;
double anchorY = (globals.getPrimaryStage().getHeight() - height) / 2;
popup.show(rootPane, JFXPopup.PopupVPosition.TOP, JFXPopup.PopupHPosition.LEFT, anchorX, anchorY);
item.getChildren().add(new TreeItem<>(new Twitter()));
contextMenu.freeActionListeners();
});
globals.getPrimaryStage()
returns the stage. globals.getPrimaryStage()
返回阶段。
rootPane
is the well ... the first Pane. rootPane
是井...第一个窗格。
Here are some values from the system.out.println()
: 这是system.out.println()
一些值:
stage - width: 800.0height: 550.4000244140625
width: 533.3333333333334 height: 366.933349609375
stage - width: 1339.199951171875height: 684.7999877929688
width: 892.7999674479166 height: 456.5333251953125
As you can see these values do change, but the size of the popup doesn't change. 如您所见,这些值确实发生了变化,但是弹出窗口的大小没有变化。
Any help would be appreciated. 任何帮助,将不胜感激。
You can center the popup stage over it's parent window using the initOwner()
method, assuming popup
is a stage: 您可以使用initOwner()
方法将弹出窗口initOwner()
其父窗口的initOwner()
,假设popup
是一个阶段:
popup.initOwner(globals.getPrimaryStage())
And it looks like you are creating a new popup
each time the action is called and setting the size based on the primaryStage
size. 看起来您每次调用该操作时都在创建一个新的popup
并根据primaryStage
大小设置大小。 If you want it to remember your own sizing, you will need to save those dimensions at some point when the popup
is closed. 如果希望它记住自己的尺寸,则需要在关闭popup
时的某些时候保存这些尺寸。
First of all I notices that getting the stage height includes the topbar ( - [] X ). 首先,我注意到获得载物台高度包括顶栏(-[] X)。 That is why the popup doesn't vertically center properly. 这就是为什么弹出窗口无法正确垂直居中的原因。 To fix that I added .getScene()
and then .getHeight()
. 为了解决这个问题,我添加了.getScene()
和.getHeight()
。
Secondly I now added the changed it to the following: I made it so that the popup is created when the button is clicked. 其次,我现在将更改添加到了以下内容:创建了它,以便在单击按钮时创建弹出窗口。 So instead of making 1 jfxpopup I just override the pointer to the popup and let the garbage collector do the rest. 因此,我没有重写1 jfxpopup,而是重写了指向弹出窗口的指针,然后让垃圾回收器完成其余的工作。
// Option add new leaf
CONTEXT_MENU.getNewLeaf().setOnAction(event -> {
popup = new JFXPopup();
try {
popup.setPopupContent((Region) UTIL
.getPopupView(UTIL.getView("view/application/popup/FormTest.fxml"), popup));
} catch (IOException e) {
e.printStackTrace();
}
popup.setAutoFix(true);
// Width & height of the popup
double width = GLOBALS.getPrimaryStage().getScene().getWidth() / 1.5;
double height = GLOBALS.getPrimaryStage().getScene().getHeight() / 1.5;
popup.getPopupContent().setPrefWidth(width);
popup.getPopupContent().setPrefHeight(height);
// X & y axis of the popup
double anchorX = (GLOBALS.getPrimaryStage().getScene().getWidth() - width) / 2;
double anchorY = (GLOBALS.getPrimaryStage().getScene().getHeight() - height) / 2;
popup.show(ROOT_PANE, JFXPopup.PopupVPosition.TOP, JFXPopup.PopupHPosition.LEFT, anchorX,
anchorY);
item.getChildren().add(new TreeItem<>(new Twitter()));
CONTEXT_MENU.freeActionListeners();
});
1 quistion : When I you say popup.hide()
does that remove the instance of that popup? 1 quistion:当我说popup.hide()
是否删除该弹出窗口的实例? Right now I just let the garbage collector do its thing. 现在,我只是让垃圾收集器执行其操作。 Just curious. 只是好奇。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.