Home / Blog / From Swing to Jwt

History always repeats itself and sometimes that's good. Here's a second chance to be part of it: tmdtc.com

From Swing to Jwt

The other day, I read the blog post Agents of Swing by Stuart Sierra describing the use of agents in combination with a Swing UI. As soon as I read it I wanted to implement it with Jwt in the hope it would be easy. And I wasn't disappointed!

As I had expected, I was able to reuse the agents code from Stuart and integrate it very easily in my web app. Actually, the development op the web interface was similar to the development of the swing interface. It's so similar I can only paste it here without further explanations:

; Create the Timer that will update results on the page 
; by calling the function passed
(defn create-timer [wapp fn-update-results]
  (let [timer (WTimer.)]
        (.setInterval timer 1000)
   (.. timer timeout (addListener wapp (create-listener [event] 

(defn make-flipper-app [env]
  (let [wapp (new WApplication env)
        root (.getRoot wapp)
        result-text (WText. "")
        flipper (new-flipper)
        fn-update-results (fn []
               (.setText result-text (
                   str "error: " (error @flipper) " after " 
                       (:total @flipper)  " flips, of which " 
                       (:heads @flipper) " where heads"  )))
        timer (create-timer wapp fn-update-results)
    (.setTitle wapp "Agents of Jwt")
    (.. (WPushButton. "Start counter" root) clicked
               (addListener  wapp ( create-listener [mouse-event] 
                   (send flipper start) 
                   (.start timer)  )))    
   (.. (WPushButton. "Stop counter" root) clicked 
              (addListener  wapp ( create-listener [mouse-event] 
                   (send flipper stop) 
                   (.stop timer) 
                   (fn-update-results)   )))
    (.addWidget  root (WBreak.))
    (.addWidget  root result-text)
    (.addWidget  root (WBreak.))

The only thing needed in addition to this is importing the necessary jwt classes, the definition of the servlet, and starting the server, the last two being done with this code:

(def my-servlet
  (proxy [WtServlet] [] 
    (createApplication [env]
      (make-flipper-app env))))

(def my-server (jetty-server))
(add-servlet my-server my-servlet "/*")


Here is the file with the complete code that I was able to run:

If someone's interested, I can possibly bundle a complete environment to be able to run it immediately. Just give me a sign.


Log in or create a user account to comment.

I'm available for hire 

If you're looking for someone to temporarily join your IT team, or if you need help to put IT to work in your company, I'm ready to help. Contact me now !

Mon Tue Wed Thu Fri Sat Sun
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31