Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 864
Gerrit Begher Avatar asked May 27 '12 16:05

Gerrit Begher


2 Answers

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))))
like image 169
Mikita Belahlazau Avatar answered Nov 11 '22 23:11

Mikita Belahlazau


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.

like image 34
Gary Verhaegen Avatar answered Nov 11 '22 23:11

Gary Verhaegen