Slightly different variation from what Howard suggested
import scala.swing._
object GUI extends SimpleGUIApplication {
def top = new Frame {
title="Test"
import javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE
peer.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE)
override def closeOperation() { showCloseDialog() }
private def showCloseDialog() {
Dialog.showConfirmation(parent = null,
title = "Exit",
message = "Are you sure you want to quit?"
) match {
case Dialog.Result.Ok => exit(0)
case _ => ()
}
}
}
}
By using DO_NOTHING_ON_CLOSE
you are given a chance to define what should be done when a WindowEvent.WINDOW_CLOSING
event is received by the scala frame. When a scala frame receives a WINDOW_CLOSING
event it reacts by calling closeOperation
. Hence, to display a dialog when the user attempts to close the frame it is enough to override closeOperation
and implement the desired behavior.