6

here's what i'd like to do:

I've got a ref that represents a list of items. I'd like to have a listbox (seesaw?) that displays this lists contents, updating automatically (whenever i change the ref).

Dan Lipsitt
  • 175
  • 1
  • 11
Gerrit Begher
  • 363
  • 2
  • 14

2 Answers2

4

You can use add-watch to add callback which will be called every time ref is modified. This callback should call method that updates listbox:

(def data (ref [1 2 3]))

(defn list-model 
  "Create list model based on collection"
  [items]
  (let [model (javax.swing.DefaultListModel.)]
    (doseq [item items] (.addElement model item))
    model))

(def listbox (seesaw.core/listbox :model [])) 

(add-watch data nil
  (fn [_ _ _ items] (.setModel listbox (list-model items))))
Mikita Belahlazau
  • 15,326
  • 2
  • 38
  • 43
4

Another way to do it is to bind the contents of the ref to the model of the listbox, using seesaw.bind.

(require [seesaw core [bind :as b]])
(def lb (listbox))
(def r (ref []))
(b/bind r (b/property lb :model))

The seesaw.bind library is well worth exploring, IMHO. The API is well documented once you have some idea how it all fits together; this blog post is a nice introduction.

Gary Verhaegen
  • 1,081
  • 9
  • 6