Skip to content

Instantly share code, notes, and snippets.

@Skinner927
Last active January 2, 2016 12:09
Show Gist options
  • Save Skinner927/8301760 to your computer and use it in GitHub Desktop.
Save Skinner927/8301760 to your computer and use it in GitHub Desktop.
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