【问题标题】:Clojure - Autoupdating ListboxClojure - 自动更新列表框
【发布时间】:2012-05-27 16:39:33
【问题描述】:

这是我想做的:

我有一个代表项目列表的 ref。我想要一个 listbox(跷跷板?)来显示此列表内容,并自动更新(每当我更改参考时)。

【问题讨论】:

    标签: clojure listbox seesaw


    【解决方案1】:

    您可以使用add-watch 添加回调,该回调将在每次修改 ref 时调用。这个回调应该调用更新列表框的方法:

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

    【讨论】:

      【解决方案2】:

      另一种方法是使用 seesaw.bind 将 ref 的内容绑定到列表框的模型。

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

      seesaw.bind 库非常值得探索,恕我直言。 API 有据可查,一旦你知道它是如何组合在一起的; this blog post 是一个很好的介绍。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-28
        • 1970-01-01
        • 2016-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多