http://clojure.org/java_interop
"Note that while method fns can be provided to override protected
methods, they have no other access to protected members, nor to super,
as these capabilities cannot be proxied."

However this solution might be adequate:

        (proxy [SwingWorker] []
                (doInBackground []
                        (try
                        (loop [m 0]
                                (prn (str "doInBackground1: m=" m))
                                (javax.swing.SwingUtilities/
invokeLater
                                  (fn []
                                    (prn (str "process: val=" m))
                                    (. tf2 (setText (str m)))))
                                (Thread/sleep 2000)
                                (if (< m 10)
                                        (recur (inc m))
                                        (prn (str "doInBackground2:
m=" m))))
                                (catch Exception e (prn e))))
                (done []
                        (prn (str "done"))
                        (. cb (setSelected true)))))

ie: don't use the publish/process part but instead put updates on the
Event Thread explicitly.


On Mar 20, 5:56 am, timc <timgcl...@gmail.com> wrote:
> I am trying to understand how to use SwingWorker to do time-consuming
> work that needs to update a Swing GUI. The following makes a frame
> with a button (which starts a SwingWorker on the first click).
>
> Thereafter, clicking the button increments an integer n that is
> displayed in the first JTextField (Event Dispatch thread).
>
> Meanwhile the worker thread running the method doInBackground()
> increments an integer m and sleeps for 2s. On each increment it calls
> publish(m) which is supposed to run in the Event Dispatch thread and
> show the current value of m in the second JTextField. When it has done
> this ten times, the exit from doInBackground() causes the done()
> method to be called (in the Event Displatch thread), which sets the
> checkbox.
>
> However the code (proxy-super publish m) throws this exception:
>
> #<IllegalArgumentException java.lang.IllegalArgumentException: No
> matching method found: publish for class
> clojure.proxy.javax.swing.SwingWorker>
>
> Can anyone tell me what I am doing wrong?
>
> (import
>         '(javax.swing JFrame JPanel JButton JTextField JCheckBox SwingWorker)
>         '(java.awt FlowLayout)
>         '(java.awt.event ActionListener)
> )
>
> (def startIt (atom true))
> (def n (atom 0))
> (def tf1 (doto (JTextField. 5) (. setEditable false)))
> (def tf2 (doto (JTextField. 5) (. setEditable false)))
> (def cb (JCheckBox.))
>
> (def worker
>         (proxy [SwingWorker] []
>                 (doInBackground []
>                         (try
>                         (loop [m 0]
>                                 (prn (str "doInBackground1: m=" m))
>                                 (proxy-super publish m)
>                                 (Thread/sleep 2000)
>                                 (if (< m 10)
>                                         (recur (inc m))
>                                         (prn (str "doInBackground2: m=" m))))
>                                 (catch Exception e (prn e))))
>                 (process [val]
>                         (prn (str "process: val=" val))
>                         (. tf2 (setText (str val))))
>                 (done []
>                         (prn (str "done"))
>                         (. cb (setSelected true)))))
>
> (def buttonAction
>         (proxy [ActionListener] []
>                 (actionPerformed [e]
>                         (when @startIt (. worker execute) (swap! startIt not))
>                         (dosync (swap! n inc))
>                         (. tf1 (setText (str @n))))))
>
> (def button
>         (doto (JButton. "Press me") (. addActionListener buttonAction)))
>
> (def frame
>         (doto   (JFrame. "SwingWorker test")
>                 (.setDefaultCloseOperation JFrame/DISPOSE_ON_CLOSE)
>                 (. add
>                         (doto (JPanel. (FlowLayout.))
>                                 (. add button)
>                                 (. add tf1)
>                                 (. add tf2)
>                                 (. add cb)))
>                 (. pack)
>                 (. setVisible true)))
--~--~---------~--~----~------------~-------~--~----~
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