Clojure - Auto Delete List

here is what i would like to do:

I have a ref that represents a list of elements. I would like to have a listbox (seeaw?) Which displays this contents of the lists is automatically updated (whenever I change ref).

+6
source share
2 answers

You can use add-watch to add a callback that will be called every time ref is changed. This callback should call a method that updates the list:

(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)))) 
+4
source

Another way to do this is to link the contents of the ref link to the list model using seeaw.bind.

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

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

+4
source

Source: https://habr.com/ru/post/916793/


All Articles