Home

Popping up a dialog

Popping up a dialog

Thanking the user by printing a message on the terminal is not quite right for a GUI program. We can fix this easily by making use of a dialog. A dialog is a window that pops up to inform the user or get some decision from the user. Most dialogs are modal: This means that while the dialog is on the screen, the program window does not react to user input anymore.

Swing defines a few standard dialogs that are very easy to use, as the following program gui4.scala shows.

import java.awt.Color
import scala.swing._

class UI extends MainFrame {
  val la = new Label("Look at me!")

  la.foreground = Color.BLUE
  title = "GUI Program #4"

  contents = new BoxPanel(Orientation.Vertical) {
    contents += la
    contents += Swing.VStrut(10)
    contents += Swing.Glue
    contents += Button("Press me, please") { pressMe() }
    contents += Swing.VStrut(5)
    contents += Button("Change text") { changeText() }
    contents += Swing.VStrut(5)
    contents += Button("Close") { closeMe() }
    border = Swing.EmptyBorder(10, 10, 10, 10)
  }

  def pressMe() {
    Dialog.showMessage(contents.head, "Thank you!", title="You pressed me")
  }

  def changeText() {
    val r = Dialog.showInput(contents.head, "New label text", initial=la.text)
    r match {
      case Some(s) => la.text = s
      case None => 
    }
  }

  def closeMe() {
    val res = Dialog.showConfirmation(contents.head, 
				      "Do you really want to quit?", 
				      optionType=Dialog.Options.YesNo,
				      title=title)
    if (res == Dialog.Result.Ok)
      sys.exit(0)
  }
}

object GuiProgramFour {
  def main(args: Array[String]) {
    val ui = new UI
    ui.visible = true
  }
}
Note that we have now defined methods in the UI class for each of the buttons. We have also stored the label as a field in the class, so that we can change the color, and the label text later.

The method Dialog.showMessage pops up a dialog to display a message. The first argument is a "parent component", which is mostly used to position the dialog on the screen. We simply use the first (and only) element of the UI's contents.

The method Dialog.showConfirmation pops up a dialog to ask a question. Its return value indicates the choice made by the user, and is one of Dialog.Result.Yes, Dialog.Result.Ok (same as Yes), Dialog.Result.No, Dialog.Result.Cancel, or Dialog.Result.Closed (if the user closed the dialog by pressing the X-button in the corner instead of one of the buttons in the dialog).

The optional argument optionType allows you to decide which buttons will be shown. The possible values are Dialog.Options.YesNo, Dialog.Options.YesNoCancel, and Dialog.Options.OkCancel.

The method Dialog.showInput allows you to request input from the user. You have to provide an initial string (use "" if you want the input field to be empty when the dialog pops up). The result is of type Option[String]. It will match Some(s) and set the variable s if the user entered a string. If the user presses "Cancel" or closes the dialog, the result is None.