Swing 列表框(JList)赢得';t从自定义ListModel动态更新
我正在Clojure中使用Seesaw开发GUI应用程序,在更新自定义ListModel时,很难更新listbox(Java中的JList) 以下是我的一些代码:Swing 列表框(JList)赢得';t从自定义ListModel动态更新,swing,clojure,jlist,seesaw,Swing,Clojure,Jlist,Seesaw,我正在Clojure中使用Seesaw开发GUI应用程序,在更新自定义ListModel时,很难更新listbox(Java中的JList) 以下是我的一些代码: (deftype ActionHistoryListModel [^{:unsynchronized-mutable true} listeners ^{:unsynchronized-mutable true} listening-to] ListModel (addListDataListener [this
(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...
其中动作历史记录
是一个参考
它转到了应该更新列表的位置,因为发生了System.out.print
,但列表框不想更新
对可能出现的问题有什么想法吗?是不是混合使用EDT和watch回调
如果需要更多的代码,请告诉我。自定义模型总是很棘手,尤其是在事件通知方面,因此很难说这会有多好的效果。也就是说,我对为什么没有通知的最佳猜测是,您使用的
map
是懒惰的,即notify
方法中的最后一个表单实际上没有做任何事情。请尝试以下方法:
(doseq [listener listeners]
(invoke-later (notification listener)))
祝你好运。这就成功了,谢谢你,戴夫!我不知道
map
是懒惰的;记住这一点很好。我不确定我是否会在最终的代码中使用这个补丁——我提出了另一个版本,它使用了DefaultListModel
,大小是(到目前为止)的1/4,但我还没有承诺。感谢您在跷跷板上的所有工作;我还没怎么用过它,但到目前为止我喜欢它!