6

I'm building a GUI with the SimpleSwingApplication trait in scala swing. What I want to do is to provide a mechanism on close, that asks the user (Yes,No,Cancel) if he didn't save the document yet. If the user hits Cancel the Application shouldn't close. But everything I tried so far with MainFrame.close and closeOperation didn't work.

So how is this done in Scala Swing?

I'm on Scala 2.9.

Thanks in advance.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Felix Dietze
  • 602
  • 7
  • 16

4 Answers4

5

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.

Mirco Dotta
  • 1,300
  • 8
  • 13
3

What about this:

import swing._
import Dialog._

object Test extends SimpleSwingApplication { 
  def top = new MainFrame { 
    contents = new Button("Hi")

    override def closeOperation { 
      visible = true
      if(showConfirmation(message = "exit?") == Result.Ok) super.closeOperation  
    }
  }
}
gerferra
  • 1,519
  • 1
  • 14
  • 26
  • Unfortunately this doesn't work (at least not when it is a MainFrame), because the confirmation dialog only appears after the main window has already disappeared. – sambe Nov 14 '14 at 10:22
2

I am not really familiar with scala swing but I found this code in some of my old test programs:

object GUI extends SimpleGUIApplication {
  def top = new Frame {
    title="Test"
    peer.setDefaultCloseOperation(0)

    reactions += {
      case WindowClosing(_) => {
        println("Closing it?")
        val r = JOptionPane.showConfirmDialog(null, "Exit?")
        if (r == 0) sys.exit(0)
      }
    }
  }
}
Howard
  • 38,639
  • 9
  • 64
  • 83
  • I am not sure: is `sys.exit(0)` normal for this purpose? At first sight it seems too heavy, but I may be wrong. – Suma Mar 24 '15 at 14:38
0

This does what I wanted to do; calling super.closeOperation didn't close the frame. I would have just said that in a comment, but I am not yet allowed.

object FrameCloseStarter extends App {
  val mainWindow = new MainWindow()
  mainWindow.main(args)
}

class MainWindow extends SimpleSwingApplication {
  def top = new MainFrame {
    title = "MainFrame"
    preferredSize = new Dimension(500, 500)
    pack()
    open()

    def frame = new Frame {
      title = "Frame"
      preferredSize = new Dimension(500, 500)
      location = new Point(500,500)
      pack()

      peer.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)

      override def closeOperation() = {
        println("Closing")
        if (Dialog.showConfirmation(message = "exit?") == Result.Ok) {
          peer.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE)
          close()
        }
      }
    }
    frame.open()
  }
}
Bday
  • 1,005
  • 7
  • 13