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).
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).
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))))
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.
© 2022 - 2024 — McMap. All rights reserved.