I'm trying to learn Swing, so I'm writing the most robust Celsius
converter app that I can. I've separated the conversion work into a
separate SwingWorker thread, so this requires Java 6. Does anyone have
any suggestions?
(ns org.sample.play-with-swing.multithreaded-celsius-converter
(:import [javax.swing JFrame JLabel JTextField JButton SwingWorker
SwingUtilities]
[java.awt.event ActionListener]
[java.awt GridLayout]))
(defn convert-c-to-f [celsius-text]
(when-not (empty? celsius-text)
(let [c (Double/parseDouble celsius-text)]
(str (+ 32 (* 1.8 c))))))
(defn c-to-f-worker [celsius-text-field fahrenheit-text-field]
(proxy [SwingWorker] []
(doInBackground []
(convert-c-to-f (.getText celsius-text-field)))
(done []
(.setText fahrenheit-text-field (.get this)))))
(defn celsius-gui []
(let [frame (JFrame. "Celsius Converter")
celsius-text-field (JTextField.)
celsius-label (JLabel. "Celsius")
convert-button (JButton. "℃ to ℉")
fahrenheit-label (JLabel. "Fahrenheit")
fahrenheit-text-field (JTextField.)]
(.addActionListener convert-button
(proxy [ActionListener] []
(actionPerformed [event]
(.execute (c-to-f-worker celsius-text-field fahrenheit-
text-field)))))
(doto frame
(.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
(.setLayout (GridLayout. 3 2 3 3))
(.add celsius-text-field)
(.add celsius-label)
(.add fahrenheit-text-field)
(.add fahrenheit-label)
(.add convert-button)
(.setSize 300 100)
(.setVisible true))))
; For thread safety, does the text-sampler-gui thread have to be
invoked from the event
; dispatch thread?
(SwingUtilities/invokeLater celsius-gui)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---