Clojure - Autoupdating Listbox
Asked Answered
M

2

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

Motherland answered 27/5, 2012 at 16:39 Comment(0)
A
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))))
Art answered 27/5, 2012 at 17:35 Comment(0)
T
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.

Thomasenathomasin answered 18/6, 2012 at 9:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.