Skip to content

Commit 3a85262

Browse files
committed
Merge pull request #13 from Skinner927/master
Center ExceptionDialog to Parent
2 parents c0b31b8 + 2ba8f9e commit 3a85262

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

javafx-dialogs/src/javafx/scene/control/Dialogs.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -489,13 +489,21 @@ public DialogOptions getDefaultOptions() {
489489
}
490490

491491
private static void centerToOwner(DialogTemplate template) {
492-
final FXDialog dialog = template.getDialog();
492+
FXDialog dialog = template.getDialog();
493493
Window window = dialog.getOwner();
494494

495+
if(!centerToOwner(window, dialog)){
496+
template.getDialog().centerOnScreen();
497+
}
498+
}
499+
500+
private static boolean centerToOwner(Window window, FXDialog inDialog) {
495501
// get center of window
496502
final double windowCenterX = window.getX() + (window.getWidth() / 2);
497503
final double windowCenterY = window.getY() + (window.getHeight() / 2);
498504

505+
final FXDialog dialog = inDialog;
506+
499507
// verify
500508
if(!Double.isNaN(windowCenterX)){
501509
// set a temp position
@@ -506,13 +514,24 @@ private static void centerToOwner(DialogTemplate template) {
506514
Platform.runLater(new Runnable() {
507515
@Override
508516
public void run() {
509-
dialog.setX(windowCenterX - (dialog.getWidth() / 2));
510-
dialog.setY(windowCenterY - (dialog.getHeight() / 2));
517+
double x = windowCenterX - (dialog.getWidth() / 2);
518+
double y = windowCenterY - (dialog.getHeight() / 2);
519+
520+
// we don't want the top left of the dialog to shoot off the screen
521+
if(x < 0)
522+
x = 0;
523+
if(y < 0)
524+
y = 0;
525+
526+
dialog.setX(x);
527+
dialog.setY(y);
511528
}
512529
});
530+
531+
return true;
513532
}
514533
else {
515-
template.getDialog().centerOnScreen();
534+
return false;
516535
}
517536
}
518537

@@ -967,7 +986,9 @@ private List<Button> createButtons() {
967986
Button detailsBtn = new Button((detailBtnStr == null) ? "" : getMessage(detailBtnStr));
968987
detailsBtn.setOnAction(new EventHandler<ActionEvent>() {
969988
@Override public void handle(ActionEvent ae) {
970-
new ExceptionDialog(dialog, throwable).show();
989+
ExceptionDialog dia = new ExceptionDialog(dialog, throwable);
990+
centerToOwner(dialog, dia);
991+
dia.show();
971992
}
972993
});
973994
buttons.add(detailsBtn);

0 commit comments

Comments
 (0)