Просмотрев веб-сайт, я не нашел решения. На рисунке ниже объясняет, что я хочу добиться:Центр Java Alert на узле JavaFX [например, StackPane или кнопка]
Одним из возможных решений был создан для компьютера с одного экрана, для нескольких экранов она нуждается в модификации. Хотя это работает [для одного экрана], я не уверен, если это правильный способ сделать это ....
Вопрос: Это правильное направление Возможно, что JavaFX не встраивать? такая функциональность для Nodes
и имеет ли она только для Stage
?
Код:
/**
* Makes a question to the user.
*
* @param text
* the text
* @param node
* The node owner of the Alert
* @return true, if successful
*/
public static boolean doQuestion(String text, Node node) {
questionAnswer = false;
// Show Alert
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.initStyle(StageStyle.UTILITY);
alert.setHeaderText("");
alert.setContentText(text);
// Make sure that JavaFX doesn't cut the text with ...
alert.getDialogPane().getChildren().stream().filter(item -> node instanceof Label)
.forEach(item -> ((Label) node).setMinHeight(Region.USE_PREF_SIZE));
// I noticed that height property is notified after width property
// that's why i choose to add the listener here
alert.heightProperty().addListener(l -> {
// Width and Height of the Alert
int alertWidth = (int) alert.getWidth();
int alertHeight = (int) alert.getHeight();
// Here it prints 0!!
System.out.println("Alert Width: " + alertWidth + " , Alert Height: " + alertHeight);
// Find the bounds of the node
Bounds bounds = node.localToScreen(node.getBoundsInLocal());
int x = (int) (bounds.getMinX() + bounds.getWidth()/2 - alertWidth/2);
int y = (int) (bounds.getMinY() + bounds.getHeight()/2 - alertHeight/2);
// Check if Alert goes out of the Screen on X Axis
if (x + alertWidth > InfoTool.getVisualScreenWidth())
x = (int) (getVisualScreenWidth() - alertWidth);
else if (x < 0)
x = 0;
// Check if Alert goes out of the Screen on Y AXIS
if (y + alertHeight > InfoTool.getVisualScreenHeight())
y = (int) (getVisualScreenHeight() - alertHeight);
else if (y < 0)
y = 0;
// Set the X and Y of the Alert
alert.setX(x);
alert.setY(y);
});
return questionAnswer;
}
И эти два метода отсутствуют выше:
/**
* Gets the visual screen width.
*
* @return The screen <b>Width</b> based on the <b>visual bounds</b> of the
* Screen.These bounds account for objects in the native windowing
* system such as task bars and menu bars. These bounds are
* contained by Screen.bounds.
*/
public static double getVisualScreenWidth() {
return Screen.getPrimary().getVisualBounds().getWidth();
}
/**
* Gets the visual screen height.
*
* @return The screen <b>Height</b> based on the <b>visual bounds</b> of the
* Screen.These bounds account for objects in the native windowing
* system such as task bars and menu bars. These bounds are
* contained by Screen.bounds.
*/
public static double getVisualScreenHeight() {
return Screen.getPrimary().getVisualBounds().getHeight();
}
Попробуйте сделать так, чтобы он работал с узлом в целом и не только с этапом. +1 для вашей попытки. – GOXR3PLUS
Спасибо @ GOXR3PLUS, теперь он работает с узлом. Исправлено также мигание при показе диалога. – cdr89
Я попробую :) – GOXR3PLUS