Search code examples
clojurelistboxseesaw

Clojure - Autoupdating Listbox


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).


Solution

  • 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))))