Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure: exit program when Window Frame is Closed

I'd like my Clojure program to exit when a JFrame is closed.

I'm attempting to trap and handle the close event as such:

(def exit-action (proxy [WindowAdapter] []
               (windowClosing [event] (fn [] (System/exit 0)))
               )
)
(.addWindowListener frame exit-action)

This doesn't throw any obvious errors but it also doesn't appear to do what I want.

Assistance is appreciated.

Answer:

Adapting Rekin's answer did the trick:

(.setDefaultCloseOperation frame JFrame/EXIT_ON_CLOSE)

Note that that is:

setDefaultCloseOperation 

not:

setDefaultOperationOnClose
like image 815
SMTF Avatar asked Mar 22 '26 11:03

SMTF


2 Answers

In Java it's:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

More elaborate examples can be found in official Java Swing tutorial about Frames

like image 113
Rekin Avatar answered Mar 25 '26 00:03

Rekin


I would use EXIT_ON_CLOSE, but the reason your first attempt didn't work is that the body of proxy should contain (System/exit 0), not (fn [] (System/exit 0)). Rather than exiting, you were returning (and then throwing away) a function that, when called, would exit.

like image 26
amalloy Avatar answered Mar 25 '26 02:03

amalloy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!