Listbox(JList)不会从自定义ListModel动态更新 [英] Listbox (JList) Won't update dynamically from custom ListModel
问题描述
我使用Seesaw在Clojure中使用GUI应用程序,并且在我的自定义ListModel更新时无法更新列表框(Java中的JList)。
I'm working on a GUI app in Clojure using Seesaw and am having trouble getting a listbox (JList in Java) to update when my custom ListModel gets updated.
这里是我的一些代码:
(deftype ActionHistoryListModel
[^{:unsynchronized-mutable true} listeners
^{:unsynchronized-mutable true} listening-to]
ListModel
(addListDataListener [this listener]
(set! listeners (conj listeners listener)))
(removeListDataListener [this listener]
(set! listeners (remove #(= % listener) listeners)))
(getSize [this]
(get-in (deref listening-to) [:count]))
(getElementAt [this index]
(get-in (deref listening-to) [:actions index]))
ActionHistoryListModelProtocol
(listen-to [this r]
(do
(set! listening-to r)
(add-watch r this (fn [_ _ _ new-state] (.notify this new-state)))))
(notify [this new-state]
(let [action ((meta new-state) :last-action)
const (cond
(= action :create) INTERVAL_ADDED
(= action :update) CONTENTS_CHANGED)
index (last ((meta new-state) :action-target))
event (ListDataEvent. this const index index)
notification (cond
(= action :create) #(.intervalAdded % event)
(= action :update) #(.contentsChanged % event))
]
(. (.. System out) print (str "Index: " index "\n" "Event: " event "\n"))
(map #(invoke-later (notification %)) listeners)))
)
(defn make-action-history-list-model []
(ActionHistoryListModel. #{} nil))
(def ahlm (make-action-history-list-model))
(.listen-to ahlm action-history)
(def undo-list (listbox :model ahlm))
; then put list in frame...
其中操作历史
是 ref
。
它到达列表应该更新的点,因为 System.out.print
正在进行,但列表框不想更新
It goes to the point where the list should be updated because the System.out.print
is happening, but the listbox doesn't want to update
走错了?
让我知道是否需要更多的代码。
Let me know if more code is needed.
推荐答案
自定义模型总是棘手的,特别是事件通知,所以很难说这将是多么好。也就是说,我最好的猜测为什么没有被通知是你使用 map
这是懒惰,即你的通知
方法实际上没有做任何事情。请改为尝试:
Custom models are always tricky especially around event notification so it's hard to say how well this will work. That said, my best guess as to why nothing is being notified is that your using map
which is lazy, i.e. the last form in your notify
method doesn't actually do anything. Try this instead:
(doseq [listener listeners]
(invoke-later (notification listener)))
祝你好运。
这篇关于Listbox(JList)不会从自定义ListModel动态更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!