5

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?

SCdF
  • 57,260
  • 24
  • 77
  • 113

2 Answers2

4

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. :)

Dave Ray
  • 39,616
  • 7
  • 83
  • 82
  • This works on a `button`, but it doesn’t work on a `listbox` that has a listener on the `:selection` event, as in the [Seesaw REPL Tutorial](https://gist.github.com/daveray/1441520#file-seesaw-repl-tutorial-clj-L195-L244). In that case, I just recreated the listbox with the same options and then set the contents of the frame to the new listbox. – Rory O'Kane Dec 16 '13 at 04:27
0

You cannot do that if you don't have that function reference. What you can do is use *1 special vara in REPL, which basically stores the result of last executed expression, to remove the handlers from the REPL.

Ankur
  • 33,367
  • 2
  • 46
  • 72
  • 1
    Yep, though in this case I'd already moved too far forward in the REPL and so lost those references too. – SCdF May 23 '13 at 08:39