Search code examples
javalibgdxscene2d

How to use Libgdx Scene2d Dialog?


I created a dialog and added a button actor.

windowStyle = new Window.WindowStyle(boldFont, Color.WHITE, drawable);
dialog = new Dialog("Error", windowStyle);
dialog.getContentTable().add(resetButton);

but the problem is that when I do dialog.show(stage) only the background of the dialog is at the center, and the string "Error" is at the top left corner of the dialog background while the resetButton is also not aligned.


Solution

  • You can do several things here. The Dialog class has at last three tables in it. The "Error" string is added to the titleTable. You could create an empty titled Dialog:

    dialog = new Dialog("", windowStyle);

    and then add and center your title manually:

    dialog.getTitleTable().add(new Label("Error", new Label.LabelStyle(boldFont, Color.WHITE)).center().expand();

    Same for the resetButton, but you could use the getContentTable() or getButtonTable() methods.

    If you want to resize or reposition your dialog window, I found the only way to do so is to create your own Dialog class, MyDialog extends Dialog and override the getPrefHeight(), getPrefWidth() and setPosition(x, y) methods.