How to interrupt the window closing mechanism in scala swing
Asked Answered
M

4

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.

Mnemosyne answered 4/7, 2011 at 14:5 Comment(0)
D
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.

Dawdle answered 4/7, 2011 at 19:26 Comment(1)
Thank you. After removing the deprecations, this did the trick.Mnemosyne
O
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  
    }
  }
}
Oldtime answered 9/7, 2011 at 3:25 Comment(1)
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.Larrylars
S
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)
      }
    }
  }
}
Sizar answered 4/7, 2011 at 17:13 Comment(1)
I am not sure: is sys.exit(0) normal for this purpose? At first sight it seems too heavy, but I may be wrong.Alyciaalyda
H
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()
  }
}
Hipolitohipp answered 18/5, 2016 at 15:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.