public
Last active

Seesaw REPL Tutorial

  • Download Gist
seesaw-repl-tutorial.clj
Clojure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
; A REPL-based, annotated Seesaw tutorial
; Please visit https://github.com/daveray/seesaw for more info
;
; This is a very basic intro to Seesaw, a Clojure UI toolkit. It covers
; Seesaw's basic features and philosophy, but only scratches the surface
; of what's available. It only assumes knowledge of Clojure. No Swing or
; Java experience is needed.
;
; This material was first presented in a talk at @CraftsmanGuild in
; Ann Arbor, MI.
;
; Dave Ray, December 2011
 
 
; First install Leiningen (https://github.com/technomancy/leiningen) and
; create a new project. In a terminal:
;
; $ lein new hello-seesaw
; $ cd hello-seesaw
; $ edit project.clj and add [seesaw "1.2.2"] to :dependencies
; $ lein deps
 
; Now let's start up the REPL and do some Seesaw stuff. Repl results are
; marked with ;=> in the usual way.
$ lein repl
 
; Here we go. First will use Seesaw and stuff
(use 'clojure.repl)
;=> nil
(use 'seesaw.core)
;=> nil
 
; Now before we create any UI stuff, tell Seesaw to try to make things look as
; native as possible. This puts the menubar in the right place on Mac, etc.
(native!)
;=> nil
 
; Now we'll start by making a frame to put stuff in. Most Seesaw widgets are
; constructed with simple functions that take :keyword/value pairs.
; (seesaw.core/frame) creates a new frame.
(def f (frame :title "Get to know Seesaw"))
;=> #'user/f
 
; So now we have a frame, but we haven't displayed it yet. Usually, we
; want to pack and show a frame. pack! just auto-sizes the frame for its
; contents
(-> f pack! show!)
;=> #<JFrame ... >
 
; Note that pack! and show! both return their argument so they can be chained.
; This is true of most Seesaw functions with side-effects.
;
; At this point you should have a very small, very boring window on your screen.
;
;
; +-----------------------------+
; | Get to know Seesaw x|
; +-----------------------------+
; | |
; +-----------------------------+
;
 
; The properties of a widget can be queried ...
(config f :title)
;=> "Get to know Seesaw"
 
; ... and modified
(config! f :title "No RLY, get to know Seesaw!")
;=> #<JFrame ...>
 
; Note that the title of the frame changed!
;
; +------------------------------+
; | NO RLY, get to know Seesaw x|
; +------------------------------+
; | |
; +------------------------------+
 
 
; So we have an empty frame. Let's give it some content
(config! f :content "This is some content")
;=> #<JFrame ... >
;
; +------------------------------+
; | NO RLY, get to know Seesaw x|
; +------------------------------+
; | This is some content |
; +------------------------------+
 
; Now we have a frame with a label in it. When Seesaw sees something like a
; string, it will create a label, mostly out of habit. Of course, we could
; create a label ourselves ...
(def lbl (label "I'm another label"))
;=> #'user/lbl
 
; and show it in the frame
(config! f :content lbl)
;=> #<JFrame ... >
 
; You know, we're going to be doing that a lot. Let's make a function
(defn display [content]
(config! f :content content)
content)
;=> #'user/display
 
(display lbl)
 
; Like the frame, a label can be manipulated with (config!). We can set some
; colors
(config! lbl :background :pink :foreground "#00f")
;=> #<JLabel ... >
 
; Seesaw knows about CSS color names and codes. Notice how we can set as many
; properties as we want with one (config!) call
 
; We can change the font too
(config! lbl :font "ARIAL-BOLD-21")
;=> #<JLabel ... >
 
; "FAMILY-STYLE-SIZE" is conventient, but using (seesaw.font/font) we get a
; little more control
(use 'seesaw.font)
;=> nil
(config! lbl :font (font :name :monospaced
:style #{:bold :italic}
:size 18))
;=> #<JLabel ... >
 
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
; So we know a little about labels now. How about a button?
(def b (button :text "Click Me"))
;=> #'user/b
 
; Sometimes we might want to show a message ...
(alert "I'm an alert")
;=> nil
 
; or get input from the user ...
(input "What's your favorite color?")
;=> "Blue"
 
; Notice that both functions block until the popup is dismissed. They both
; take an additional, initial argument which indicates which frame they're
; associated with. Otherwise, they'll just pop up in the middle of your screen
 
 
; Let's get back to our button and get him doing something...
(display b)
;=> #<JButton>
;
; +------------------------------+
; | NO RLY, get to know Seesaw x|
; +------------------------------+
; |+----------------------------+|
; || ||
; || Click Me ||
; || ||
; |+----------------------------+|
; +------------------------------+
;
; Click it. Nothing happens.
 
; Seesaw's (listen) function let's us register handler functions for events on
; a widget. All buttons support an :action event. So,
(listen b :action (fn [e] (alert e "Thanks!")))
;=> #<core$juxt$fn__3775 clojure.core$juxt$fn__3775@2e46638f>
 
; Now we can click the button and see something happen. The 'e' parameter
; is an event object. Most of the time in Seesaw, an event object can be used
; as a proxy for the widget that triggered the event. In this case, the button
; becomes the "parent" of the alert, so it's placed nicely over our frame.
 
; Also note that (listen) returned a function. Calling this function will
; undo the effects of the (listen) call, i.e. unregister the listener
(*1)
;=> [...]
 
; Now clicking does nothing again.
 
; (listen) can register multiple event handlers at once
(listen b :mouse-entered #(config! % :foreground :blue)
:mouse-exited #(config! % :foreground :red))
;=> #<core$juxt$fn__3775 clojure.core$juxt$fn__3775@2e46638f>
 
; Move the mouse over the buttons to see the text color change. Again note
; that we're using the event parameter as a proxy for the button. This
; time with (config!).
 
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
; Now let's talk about a slightly more interesting widget, a listbox.
(def lb (listbox :model (-> 'seesaw.core ns-publics keys sort)))
;=> #<JList ...>
(display lb)
;=> #<JList ...>
;
; +------------------------------+
; | NO RLY, get to know Seesaw x|
; +------------------------------+
; |#abtract-panel################|
; | action |
; | add! |
; | add!* |
; | alert |
; +------------------------------+
 
; (listbox)'s most important option is :model which can take any (finite)
; sequence and display it. Items are displayed using (str) unless a custom
; :renderer is used.
 
; This listbox is ok, but our list doesn't fit in the frame and there's no
; scrollbars. You can add scrolling to most widgets with (scrollable) ...
(display (scrollable lb))
;=> #<JScrollPane ... >
 
; Now that we have a list, it'd be nice to know what its selection is. Seesaw
; has a unified nice selection interface. If there's no selection we get nil
(selection lb)
;=> nil
 
; Otherwise, we get the value:
(selection lb)
;=> abstract-panel
(type *1)
;=> clojure.lang.Symbol
 
; Note we get the same objects out that we put in the :model above.
 
; If we want multi-selection (shift/ctrl-click)
(selection lb {:multi? true})
;=> (action add! add!* alert)
 
; Similarly, we can set the selection
(selection! lb 'all-frames)
;=> #<JList ... >
 
; was all-frames selected for you too?
 
; Finally, we might want to know when the selection changes. Everybody
; supports the :selection event
(listen lb :selection (fn [e] (println "Selection is " (selection e))))
;=> #<core$juxt$fn__3775 clojure.core$juxt$fn__3775@2e46638f>
 
; click around on the listbox and watch the selection print out.
 
; and unregister as usual
(*1)
;=> [...]
 
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
; Now we come to editable text widgets. It's easy to make one
(def field (display (text "This is a text field.")))
;=> #<JTextField ... >
;
; +------------------------------+
; | NO RLY, get to know Seesaw x|
; +------------------------------+
; | |
; | |
; | This is a text field| |
; | |
; | |
; +------------------------------+
 
; A text field is a single line of text. You can query the text...
(text field)
;=> "This is a text field."
 
; ... and change it programmatically
(text! field "A new value")
;=> #<JTextField ... >
 
; ... and you can set the font, etc as usual with config
(config! field :font "MONOSPACED-PLAIN-12" :background "#f88")
;=> #<JTextField ... >
 
 
; If you've got more than one line, you can make it multi-line...
(def area (text :multi-line? true :font "MONOSPACED-PLAIN-14"
:text "This
is
multi
line
text"))
;=> #<JTextArea ... >
 
(display area)
;=> #<JTextArea ... >
;
; +------------------------------+
; | NO RLY, get to know Seesaw x|
; +------------------------------+
; | This |
; | is |
; | multi |
; | line| |
; +------------------------------+
 
 
; If you've got a reader, URL, or anything else slurp-able, you can fill up the
; text area with it
(text! area (java.net.URL. "http://clojure.com"))
;=> #<JTextArea ... >
 
; Of course, we need scrollbars now
(display (scrollable area))
;=> #<JTextArea ... >
 
; Like selection, Seesaw has a unified scrolling API. You can scroll to the top ...
(scroll! area :to :top)
;=> #<JTextArea ... >
 
; ... or the bottom ...
(scroll! area :to :bottom)
;=> #<JTextArea ... >
 
; ... or to a particular line ...
(scroll! area :to [:line 50])
;=> #<JTextArea ... >
 
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
; Let's try showing two widgets at once. One option is a splitter:
(def split (left-right-split (scrollable lb) (scrollable area) :divider-location 1/3))
;=> #'user/split
 
(display split)
;=> #<JSplitPane ... >
;
;
; +-------------------------------------+
; | NO RLY, get to know Seesaw x|
; +-----------+-------------------------+
; |abstract-pa| This |
; |action | is |
; |add!#######| multi |
; |add!* | line| |
; +-----------+-------------------------+
;
 
; This shows both our listbox on the left and text area on the right with a
; movable splitter. If you're familiar with Swing, you'll recognize what an
; achievement :divider-location is.
 
; Let's hook them together. First a function to grab a doc string
(defn doc-str [s] (-> (symbol "seesaw.core" (name s)) resolve meta :doc))
;=> #'user/doc-str
 
; ... and now the usual selection handler
(listen lb :selection
(fn [e]
(when-let [s (selection e)]
(-> area
(text! (doc-str s))
(scroll! :to :top)))))
;=> #<JList ... >
 
; note how we have to be sure to check for nil selection
 
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
; Ok. We're pushing on the limits of what we can comfortably type in a REPL,
; so just a couple more things ...
 
; Imagine we wanted to show source as well as docs. A set of radio button might be
; a nice way to switch betwen them ...
 
(def rbs (for [i [:source :doc]]
(radio :id i :class :type :text (name i))))
;=> #'user/rbs
 
; we have a couple radio buttons. Let's add them to our frame. We'll use a
; border-panel to lay things out. This is what a border-panel layout looks like:
;
; +------------------------------------------+
; | :north |
; | |
; +----------+-------------------+-----------+
; | | | |
; | | | |
; | :west | :center | :east |
; | | | |
; | | | |
; +----------+-------------------+-----------+
; | |
; | :south |
; +------------------------------------------+
;
 
 
(display (border-panel
:north (horizontal-panel :items rbs)
:center split
:vgap 5 :hgap 5 :border))
;=> #<JPanel ...>
 
; The :vgap, :hgap, and :border options just make things look a little nicer.
 
; If you click the radio buttons, you'll notice a little problem. They're not
; mutually exclusive. We'll put them in a button group to fix that, but first
; a small detour...
 
; How can we get to the radio buttons without using the rbs var? How about
; with a selector:
(select f [:JRadioButton])
;=> (#<JRadioButton ... > #<JRadioButton ... >)
 
; or, since we gave them a :class option above ...
(select f [:.type])
;=> (#<JRadioButton ... > #<JRadioButton ... >)
 
; or, we can get them individually by :id
(select f [:#source])
;=> #<JRadioButton ... >
 
; Selectors are always enclosed in a vector.
 
; Now, about those buttons. We'll need a button group...
(def group (button-group))
;=> #'user/group
 
; It happens that (config!) can take a sequence as its first argument in addition
; to a single widget ...
(config! (select f [:.type]) :group group)
;=> (#<JRadioButton ... > #<JRadioButton ... >)
 
; Now our buttons are nice and mutex-y. A button group, like most things, has a
; selection too, whichever button is currently selected:
(selection group)
;=> #<JRadioButton ...>
 
; Combine this with (id-of) and we have our original keywords back:
(-> group selection id-of)
;=> :source
(-> group selection id-of)
;=> :doc
 
; and, of course, you can register a listener for group selection changes
(listen group :selection
(fn [e]
(when-let [s (selection group)]
(println "Selection is " (id-of s)))))
 
; Now open hello-seesaw.core.clj and see if you can put this all together into
; an app.
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; For more info see the Seesaw wiki:
; https://github.com/daveray/seesaw/wiki
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

just a small issue, shouldn't line 359 be (-> area ?

Good catch. Fixed. Thanks!

Thanks for a really enjoyable tutorial.

One minor issue I found was that I had to provide a value for the border option on line 405.

Oops. Fixed. Thanks, and I'm glad you found it enjoyable.

awesome tutorial- thanks!

Really Really cool tutorial.
After going through it, I found three minor issues with my configuration (clojure 1.3.0 and Seesaw 1.3.0):

  • (f pack! show!) was not working, had to do (show! (pack! f))
  • once I did that, it was displaying something but the window that was popping up was so small I had to resize it with (config! f :size [300 :by 300])
  • the line (def field (display (text "something"))) was not working properly (it was working, but then I couldn't change the text). Instead I had to do: (def field (text "something")) and later (display field).

Nevertheless, this tutorial is AWESOME!

Use the threading macro: (-> f pack! show!)

When I execute line 244:
(listen lb :selection (fn [e] (println "Selection is " (selection e))))

And click on the the individual items in the listbox, I see two events triggered rather than one. However, when I use my keyboard, I see only one event triggered. I am guessing because one list item is being unselected and the 2nd one is selected, but I am not sure why the behavior is not the same with the keyboard. Anyone care to enlighten?

In Swing lists (and sliders and a bunch of other things) have the concept of "isAdjusting" for certain events. The idea is if you press the mouse button down and drag it along the listbox, the selection will change and fire an event for each change. Each of these events will have isAdjusting=true. Once you release the mouse button and the box has settled on a final selection, one last event will be fired with isAdjusting=false. With this, an app can presumably avoid doing unnecessary work if the selection is just going to change again very soon anyway. It's more common to see this with sliders.

So, when you use the mouse you're seeing an event for each value of isAdjusting. With the keyboard, this doesn't happen so there's just a single event. I can't really say Seesaw should do anything differently with these events or not.

Hope this helps and thanks for trying out the tutorial,

Dave

Thanks for the reply and the tutorial. I found working through it very enjoyable.

I guess a way to avoid the multiple events is to filter out events with isAdjusting=true.

Since you probably have a lot of more experience using Swing than me, have you ever dealt with a scenario in which you would prefer to use the isAdjusting=true events?

It's actually fairly common to let all the events through. Imagine adjusting a color or opacity with a slider. There you want to see the change while its adjusting not just at the end. It might be interesting to have a set of higher-order event filter functions that can wrap event handlers and deal with this stuff. I'd have to look around to see if there are other cases like this where it would make sense.

Hmm, I got a tripple fireing of the events when adding event handlers. Could be me that makes something wrong, starts to get late here anyway...

Am getting nil returned when I do (selection group) on line 436. Using Seesaw 1.4.0. Everything working great up till this point. Anyone know whats going wrong?

Thanks for trying it out. Are any of the radio buttons selected? I guess I should add a comment in there about clicking one of them first.

Thanks its working fine now, I needed to select the radio button. Thanks for providing the tutorial.

This is a good tutorial, but I am having multiple issues when I use the latest version of seesaw--have the rules changed in later versions? For one thing, the initial call to pack does not work when I only define the title. The size is not appropriate at all, it is barely large enough to see! The size of the frame remains fixed and does not change when widgets are added. I have to call pack each time after I add a widget for the size of the frame to change appropriately. When I try to call (*1) to remove a listener, I receive this error

 ClassCastException clojure.lang.PersistentList cannot be cast to clojure.lang.IFn     user/eval6485(NO_SOURCE_FILE:1)

I am receiving a lot of errors toward the beginning of the tutorial, what am I doing wrong?

I had the same issue that some commenters are having about the initial size of the window being super small - not really sure why.

Thanks for a great tutorial! It really got me on my way and coincidentally my first clojure project is a little app that uses a very similar layout to this. That's pretty handy :-)

Thanks so much for this tutorial @daveray, it's brilliant! Was able to follow through without any problems at all, and I'm completely new to Clojure and only vaguely familiar with Swing.

Maybe you could update line 20 to version 1.4.3 of seesaw?

Hello. I'm new to Clojure and Seesaw. Thank you very much for the tutorial.

When I type (use 'seesaw.core) I receive this error

CompilerException java.lang.RuntimeException: java.lang.NoSuchFieldException: PLAİN, compiling:(font.clj:32) 

I'm on a Turkish Macbook. What I've noticed in the error message is that İ in word PLAİN is Turkish capital i. Can it be an encoding issue? Sorry if it is a stupid question :)

Found an error: after inputted line 244, then select the "canvas" item, the lein REPL says:
Selection is canvas
Selection is canvas
(Leiningen 2.1.3, Clojure 1.5.1, Seesaw 1.4.3, Ubuntu 12.04 AMD64, Oracle JDK 1.7.0_21)

Working through this on lunch break, very nice tutorial. Thanks!

Very, very awesome.

This is a nice, self-contained tutorial.

We need more instruction like this. The kind that doesn't try to explain everything in detail, but throws out a little bit of guidance, while letting the fingers and the brain get comfortable enough with certain forms. From there, we can start exploring & building, without having to understand everything, until curiosity or circumstance requires it.

Thanks, Dave.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.