user2499946
user2499946

Reputation: 689

JavaFX Custom Dialogs not centered

I am using the latest version of the dialogs for JavaFX found from here: http://code.makery.ch/blog/javafx-8-dialogs/.

My problem is quite simple: if I create a custom dialog (or use an information dialog with many lines of text separated with '\n'), the dialog is not centered to the Stage window vertically, since there appears to be a bug(?) that the content of the dialog is not completely taken in account in the centering of the dialog.

Anyone else encountered this same issue? I haven't really investigated a lot, but the Dialog class does not seem to have any method to center the dialog manually.

Upvotes: 0

Views: 2604

Answers (2)

user2499946
user2499946

Reputation: 689

I should've read the docs more carefully. There is a getWindow()-method, which allows me to centre the dialog manually. However, I think this should be default behaviour.. here's a quick and dirty way to centre any dialog:

public static final class Utils {

  public static Stage getStage() {
      return StageHelper.getStages().get(0);
  }

  public static void centre(Dialog dialog) {
      Window window = dialog.getWindow();
      Stage scene = Utils.getStage();
      double x = scene.getX() + scene.getWidth() / 2 - window.getWidth() / 2;
      double y = scene.getY() + scene.getHeight() / 2 - window.getHeight() / 2;
      window.setX(x);
      window.setY(y);
  }
}

...

Platform.runLater(() -> { Utils.centre(dialog); }
dialog.show();

Upvotes: 0

Tomas Bisciak
Tomas Bisciak

Reputation: 2841

I just tested this, You are correct , i think whoever designed these dialogs didnt take this to account while doing them.Or there was a focus on having confirmation control on center. You can make your own implementation where you check how many line separators string has and move dialog accordingly

tested on :

  • ControlsFX 8.0.6_20

I think best way to go around this is to use Exception dialog, and change its visual ,use area which is for exception, for your Full message* option.

On the page you provided there is a link on lower level api where yo ucan scroll down to Custom dialog ,Encapsulate whole custom dialog in container/stage and use main application window as a parent where popup shoud be centered(or just center of the screen).Then you can make stage undecorated and there will be no visual difference from normal dialog.

There is a yet another alternative for you, I used to use jfxmessagebox library before and you will not encounter problem there with centering.Everything works fine.Useage is similair/same to swing JOption panes.

Upvotes: 1

Related Questions