Seesaw, my Clojure/Swing experiment has been making a lot of progress. Earlier today I added basic support for JTree which is kind of the last major Swing component that had no support in Seesaw. As an test, I thought I’d try making a file explorer with the directory tree on the left and file list on the right. It took about 20 minutes and was way less tedious than the equivalent raw Swing code you’d write in Java or Clojure.
The full code for the example can be found on github.
Here’s what it ended up looking like:
There are three code highlights.
Set up a model for the tree using the same pattern as (cloure.core/tree-seq):
; Make a model for the directory tree (def tree-model (simple-tree-model #(.isDirectory %) (fn [f] (filter #(.isDirectory %) (.listFiles f))) (File. ".")))
Set up the structure of the frame. I think this reads pretty nicely:
(frame :title "File Explorer" :width 500 :height 500 :pack? false :content (border-panel :border 5 :hgap 5 :vgap 5 :north (label :id :current-dir :text "Location") :center (left-right-split (scrollable (tree :id :tree :model tree-model :renderer render-file-item)) (scrollable (listbox :id :list :renderer render-file-item))) :south (label :id :status :text "Ready")))
Set up selection event handling:
(listen (select [:#tree]) :selection (fn [e] (if-let [dir (-> (selection e) first last)] (let [files (.listFiles dir)] (config (select [:#current-dir]) :text (.getAbsolutePath dir)) (config (select [:#status]) :text (format "Ready (%d items)" (count files))) (config (select [:#list]) :model files))))))
Here, the widgets are being retrieved by id with the (select) function. Alternatively, I could have just bound them to variables and used them directly. I’m still thinking about how this should work.
This is obviously still a toy (no sorting, doesn’t update, etc, etc), but I think shows promise. I like the separation of the structure and behavior, but the selector stuff needs more thought. Probably the biggest gain over raw Swing is that Seesaw takes care of all the yucky reify and proxy stuff for you. In this example alone, a raw implementation would need two (reify)s and a (proxy) call. Building up the widget structure is also pretty tedious with raw Swing.
I’d love to to hear feedback and criticism of the API since this is my first attempt at real Clojure code. Thanks!