Handling events

Dave Ray edited this page Feb 5, 2012 · 3 revisions

Seesaw provides a unified event registration abstraction that eliminates the need to know about lots of different "listener" interfaces and registration methods. All events are identified by a keyword and handled by a client-provided handler function.

Event handler functions are single-argument functions that take an event object whose type depends on the event being fired, e.g. MouseEvent. Event handlers are installed with the (listen) function. Its first argument is a widget, or sequence of widgets, and then one or more event specs of the form event-name/function. For the most part, the name of the event is the name of a Swing event listener method. Here's an example that listens for some mouse events, assuming that p is bound to a widget:

(listen p
  :mouse-clicked (fn [e] ... do something ...)
  :mouse-entered (fn [e] ... do something ...)
  :mouse-exited  (fn [e] ... do something ...))

Note that these same arguments can be given to the :listen property when the widget is constructed:

(canvas ... :listen [:mouse-clicked (fn [e] ...) ...])

(listen) returns a function which, when called, will remove all listeners installed by the (listen) call. There is no "remove-listener" function.


You might be asking yourself this question. As with widget options the (seesaw.dev/show-events) function will give details of the events supported by a widget. So in the repl:

user=> (use 'seesaw.core)
user=> (use 'seesaw.dev)
user=> (show-events (label))
:component [java.awt.event.ComponentListener]
:focus [java.awt.event.FocusListener]
:key [java.awt.event.KeyListener]
:mouse [java.awt.event.MouseListener]
:mouse-motion [java.awt.event.MouseMotionListener]
:mouse-wheel [java.awt.event.MouseWheelListener]
:property-change [java.beans.PropertyChangeListener]

Note that events are grouped. Registering for just :mouse is equivalent to registering the same handler for all the events listed under :mouse.

See seesaw.events/listen for more details.