(ns cara.gui.dialog-test
(:import [javax.swing JDialog JTextField JButton AbstractAction]))
(defn- create-and-show [data-ref]
(let [dialog (new JDialog (@data-ref :owner) true) ;;second parameter
says block on setVisible
text-field (doto (new JTextField)
(.setColumns 40)
(.setText (@data-ref :text)))
ok-btn (doto (new JButton "OK")
(.addActionListener
(proxy [AbstractAction] []
(actionPerformed [evt]
(dosync (alter data-ref assoc
:text (.getText text-field)))
(.dispose dialog)))))]
(doto dialog
(.setLayout (new java.awt.FlowLayout))
(.add text-field)
(.add ok-btn)
(.pack)
(.setVisible true))
(@data-ref :text)))
(defn user-string [owner base-string]
(create-and-show (ref {:owner owner :text base-string})))
cara.gui.dialog-test=> (user-string nil "hallo world")
;; a dialog shows up, i change the text then press ok
"hello world"
cara.gui.dialog-test=>
I don't like this very much as there's mutable state in the data-ref,
but it does the job.
If someone else have a better idea, let me know!
hth,
Sacha