Dialog
Table of contents
This section showcases the different types of Dialogs in the BGW framework. A Dialog can be used to display a popup informing the user about warnings, and errors or text.
Dialog creation
The Dialog class provides two public constructors for two different types of dialogs:
Information Dialog
In the information dialog’s constructor the type of the dialog can be declared by the DialogType as
INFORMATION
WARNING
ERROR
CONFIRMATION
NONE
which directly affects the displayed icon and default buttons. The buttons can be altered by passing ButtonTypes.
Note that EXCEPTION
may not be used, as it is created through the second constructor here. Additionally, the title
of the popup, the header
and the content
parameters have to be passed. The following code example creates a warning dialog informing the user about an empty player name:
Dialog(
alertType = AlertType.WARNING,
title = "Warning",
header = "Empty player name",
message = "Player name must not be empty!"
)
Exception dialog
To display an exception stack trace the second constructor can be used. It takes a title
, header
, and message
, as well as the exception
to display. It contains an expandable content for the exception stack trace.
Dialog(
title = "Exception Dialog",
header = "Exception",
message = "An exception Dialog.",
exception = IllegalArgumentException("IllegalArgument passed.")
)
An example with all dialog types can be found here:
Showing a dialog
To show a dialog the method #showDialog in BoardGameApplication has to be used. The operation blocks user input until the dialog is closed. The function returns an Optional containing the chosen ButtonType. The Optional is empty if the dialog is closed by the X or in any other way aside selecting any button.
Alternatively a dialog may be shown by #showDialogNonBlocking which shows the Dialog without blocking further thread execution. This functions returns Unit
.
val dialog: Dialog = Dialog(
alertType = AlertType.CONFIRMATION,
title = "Confirmation required",
header = "Confirmation",
message = "Do you really want to proceed?"
)
showDialog(dialog).ifPresentOrElse({
if (it == ButtonType.YES) {
//Button 'YES' was clicked
} else {
//Button 'NO' was clicked
}
}) {
//Dialog was closed
}