Thanks for the response. Yeah, I used SwingWorker just for education's
sake. I'm just trying to figure out the best practices to do for
Clojure/Swing in the future. The last question was about something I
read on one of Sun's Swing tutorials--the text-sampler-gui function
calls a bunch of Swing constructors like JLabel's, and I read
somewhere that I should do those things only in the "event dispatch
thread", and that SwingUtilities.invokeLater(Runnable) would make that
work. But I'm not sure if I get it yet; why would I need to do that?
Is it always better to invokeLater in the main thread?


On Jan 27, 9:08 am, Keith Bennett <keithrbenn...@gmail.com> wrote:
> samppi -
>
> I don't suggest using SwingWorker for this unless you just want to
> practice using it for education's sake.  The calculation time is
> effectively zero in an application like this where actions are user-
> triggered, so using SwingWorker is a case of unnecessary and premature
> optimization...and the calculation is so trivial, the overhead of
> starting a new thread is likely to exceed the time it takes to perform
> the calculation.
>
> I'm not sure I understand your question at the end of the message,
> because I don't know what you are referring to by the "text-sampler-
> gui" thread, but I assume you mean the entire Swing app...if so, then
> no, you can start a Swing GUI from the main thread, and it will
> automatically start and use the UI thread.  Usually that consists of
> calling the frame's setVisible method, which starts everything going.
>
> - Keith Bennett
>
> On Jan 24, 11:45 pm, samppi <rbysam...@gmail.com> wrote:
>
> > 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 clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to