ScalaFX – Alerts and Dialogs

One of the new features on ScalaFX 8.0.40 are Alerts and Dialogs. Dialog API allows for opening a dialog window and returning result from the user. The result can be as simple as the type of button used to close the dialog. Custom dialog allows for returning an arbitrary result.

We will present examples of ScalaFX Alerts and Dialogs based on examples presented in JavaFX Dialogs blog post.

Simple Alerts

Information Alert

There are several predefined dialogs called alerts that can be easily presented to the user. A simplest alert can be shown with a single line of code:

 new Alert(AlertType.Information, "Hello Dialogs!!!").showAndWait()

013 - simple information alert

In general use you will typically customize it a bit more:

new Alert(AlertType.Information) {
  title = "Information Dialog"
  headerText = "Look, an Information Dialog"
  contentText = "I have a great message for you!"

013 - information alert

initOwner() specifies the owner for a dialog. It is not required, but specifying an owner is a good style. It allows the dialog to use the same icon as the owner. It also will let block the parent when dialog is shown modal.

Warning Alert

new Alert(AlertType.Warning) {
  title = "Warning Dialog"
  headerText = "Look, an Warning Dialog."
  contentText = "Careful with the next step!"

013 - warning alert

Error Alert

new Alert(AlertType.Error) {
  title = "Error Dialog"
  headerText = "Look, an Error Dialog."
  contentText = "Ooops, there was an error!"

013 - error alert

Confirmation Alert

Alerts and dialogs can be used to query user for information. Every alert returns type of button that was pressed to close the dialog window. A simplest form is a confirmation dialog that indicates whether user pressed OK or Cancel buttons. Strictly speaking a dialog returns an Option containing type of button pressed or None.

// Create and show confirmation alert
val alert = new Alert(AlertType.Confirmation) {
  title = "Confirmation Dialog"
  headerText = "Look, a Confirmation Dialog."
  contentText = "Are you ok with this?"

val result = alert.showAndWait()

// React to user's selectioon
result match {
  case Some(ButtonType.OK) => println("OK")
  case _                   => println("Cancel or closed")

013 - confirmation alert

You can create a dialog returning any content, an example will be shown later. First let see how to use custom buttons in an alert.

Alerts with Custom Buttons

We can customize the button in an alert by defining ButtonType objects and passing them to Alert’s buttonTypes property. Notice that we overwrite the content of the property (rather than append to it):

    val ButtonTypeOne = new ButtonType("One")
    val ButtonTypeTwo = new ButtonType("Two")
    val ButtonTypeThree = new ButtonType("Three")

    val alert = new Alert(AlertType.Confirmation) {
      title = "Confirmation Dialog with Custom Actions"
      headerText = "Look, a Confirmation Dialog with Custom Actions."
      contentText = "Choose your option."
      // Note that we override here default dialog buttons, OK and Cancel, with new ones.
      buttonTypes = Seq(ButtonTypeOne, ButtonTypeTwo, ButtonTypeThree, ButtonType.Cancel)

    val result = alert.showAndWait()

    result match {
      case Some(ButtonTypeOne)   => println("... user chose "One"")
      case Some(ButtonTypeTwo)   => println("... user chose "Two"")
      case Some(ButtonTypeThree) => println("... user chose "Three"")
      case _                     => println("... user chose CANCEL or closed the dialog")

013 - confirmation custom alert

Alerts with Custom Content

You are not limited to simple text in an alert. You can add your custom content. For instance, there is no predefined Alert for showing exceptions, but you can add your own implementation:

// Create expandable Exception.
val exceptionText = {
  val ex = new FileNotFoundException("Could not find file blabla.txt")
  val sw = new StringWriter()
  val pw = new PrintWriter(sw)
val label = new Label("The exception stacktrace was:")
 val textArea = new TextArea {
  text = exceptionText
  editable = false
  wrapText = true
  maxWidth = Double.MaxValue
  maxHeight = Double.MaxValue
  vgrow = Priority.Always
  hgrow = Priority.Always
val expContent = new GridPane {
  maxWidth = Double.MaxValue
  add(label, 0, 0)
  add(textArea, 0, 1)

new Alert(AlertType.Error) {
  title = "Exception Dialog"
  headerText = "Look, an Exception Dialog."
  contentText = "Could not find file blabla.txt!"
  // Set expandable Exception into the dialog pane.
  dialogPane().expandableContent = expContent

013 - exception alert

Text Input Dialog

You can get simple text input using the TextInputDialog. It works similar to alerts, but it returns an Option containing the text entered by the use (alerts return the button pressed):

val dialog = new TextInputDialog(defaultValue = "walter") {
  title = "Text Input Dialog"
  headerText = "Look, a Text Input Dialog."
  contentText = "Please enter your name:"

val result = dialog.showAndWait()

result match {
  case Some(name) => println("Your name: " + name)
  case None       => println("Dialog was canceled.")

013 - text input dialog

Choice Dialog

There is also a predefined dialog for selecting from a list of available choices: ChoiceDialog.The list can be collection of arbitrary objects. The choice dialog will return Option selected by the user. You create the dialog by specifying default choice and the collection of available choices.

val choices = Seq("a", "b", "c")

val dialog = new ChoiceDialog(defaultChoice = "b", choices = choices) {
  title = "Choice Dialog"
  headerText = "Look, a Choice Dialog."
  contentText = "Choose your letter:"

val result = dialog.showAndWait()

result match {
  case Some(choice) => println("Your choice: " + choice)
  case None         => println("No selection")

013 - choice dialog

Custom Dialog

Custom dialogs are created using Dialog class. Below is an example of a login dialog. Class Result defines result returned by the dialog. The dialog contains a custom graphic, two input fields (“Username” and “Password”), and custom buttons (“Login” and “Cancel”).

case class Result(username: String, password: String)

// Create the custom dialog.
val dialog = new Dialog[Result]() {
  title = "Login Dialog"
  headerText = "Look, a Custom Login Dialog"

// Set the button types.
val loginButtonType = new ButtonType("Login", ButtonData.OKDone)
dialog.dialogPane().buttonTypes = Seq(loginButtonType, ButtonType.Cancel)

// Create the username and password labels and fields.
val username = new TextField() {
  promptText = "Username"
val password = new PasswordField() {
  promptText = "Password"

val grid = new GridPane() {
  hgap = 10
  vgap = 10
  padding = Insets(20, 100, 10, 10)

  add(new Label("Username:"), 0, 0)
  add(username, 1, 0)
  add(new Label("Password:"), 0, 1)
  add(password, 1, 1)

// Enable/Disable login button depending on whether a username was entered.
val loginButton = dialog.dialogPane().lookupButton(loginButtonType)
loginButton.disable = true

// Do some validation (disable when username is empty).
username.text.onChange { (_, _, newValue) => 
  loginButton.disable = newValue.trim().isEmpty

dialog.dialogPane().content = grid

// Request focus on the username field by default.

// When the login button is clicked, convert the result to a username-password-pair.
dialog.resultConverter = dialogButton =>
  if (dialogButton == loginButtonType) Result(username.text(), password.text())
  else null

val result = dialog.showAndWait()

result match {
  case Some(Result(u, p)) => println("Username=" + u + ", Password=" + p)
  case None               => println("Dialog returned: None")

013 - custom login dialog


ScalaFX 8.0.40-R8 brings support for Alerts and Dialogs. There are several predefined dialogs: Information, Warning, Error, Confirmation, Text Input, and Choice. Predefined dialog allow some level of customization of their content and buttons. Source code for the examples of pre-defined dialogs, including customization, are in DialogsDemo.

Completely customized dialogs can be created using Dialog class. Source code for a custom login dialog is in LoginDialogDemo.

To use ScalaFX 8.0.40 add following to your SBT:

libraryDependencies += "org.scalafx" %% "scalafx" % “8.0.40-R8”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: