Last active
January 2, 2016 12:09
-
-
Save Skinner927/8301760 to your computer and use it in GitHub Desktop.
JavaFX Dialogs center on owner https://github.com/marcojakob/javafx-ui-sandbox/blob/master/javafx-dialogs/src/javafx/scene/control/Dialogs.java#L501
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static DialogResponse showDialog(DialogTemplate template) { | |
try { | |
final FXDialog dialog = template.getDialog(); | |
Window window = dialog.getOwner(); | |
// get center of window | |
final double windowCenterX = window.getX() + (window.getWidth() / 2); | |
final double windowCenterY = window.getY() + (window.getHeight() / 2); | |
// verify | |
if(!Double.isNaN(windowCenterX)){ | |
// set a temp position | |
dialog.setX(windowCenterX); | |
dialog.setY(windowCenterY); | |
// Since the dialog doesn't have a width or height till it's shown, calculate its position after it's shown | |
Platform.runLater(new Runnable() { | |
@Override | |
public void run() { | |
dialog.setX(windowCenterX - (dialog.getWidth() / 2)); | |
dialog.setY(windowCenterY - (dialog.getHeight() / 2)); | |
} | |
}); | |
} | |
else { | |
template.getDialog().centerOnScreen(); | |
} | |
template.show(); | |
return template.getResponse(); | |
} catch (Throwable e) { | |
return CLOSED; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment