Search code examples
clojureseesaw

How can I remove listeners from an object in Seesaw if I haven't kept the return function?


To add a listener to a UI element in Seesaw you do this:

(listen ui-element :action (fn [_] (...)))

listen attaches a listener that calls the provided function when :action is triggered on `ui-element1. It also returns a function. If you execute that function it removes the listener that was added with the original call.

I've been prototyping UIs in the REPL using Seesaw, and I haven't kept the return values from listen.

If I don't have the returned function, how can I remove listeners?


Solution

  • You can manually remove listeners in the following crude way:

    user=> (def b (button :text "HI"))
    user=> (listen b :action #(alert % "HI!"))
    user=> (-> (frame :content b) pack! show!)
    ; click the button, see the alert
    ; manually remove listeners
    user=> (doseq [l (.getActionListeners b)] (.removeActionListener b l))
    ; click the button, nothing happens
    

    You could put this in a helper function and use it whenever. Having this built-in somehow to seesaw.event or seesaw.dev would also be nice. Patches welcomed. :)