Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complex GUI in clojure

Tags:

clojure

swing

I just started using clojure today (however , I have used Java a lot and know of functional paradigms) and I was wondering if it was a good idea to build a clojure app with a reasonable complex interface (dragging, dropping, panning, zooming,...) using Swing?

I can imagine that a lot of the normal swing logics (especially concerning OO) has to be bypassed one way or the other..

I asume that all is possible , but is it possible in a way that justifyable?

I mean wouldn't it be like hitting a nail with a screwdriver in stead of with a hammer?

Has anyone here have experience in building GUI's with Clojure (and of Course : is Swing the ideal candidate for that?)

Thanks !

like image 454
Peter Avatar asked Feb 04 '11 20:02

Peter


2 Answers

I've found it relatively easy to use Swing to build decent user interfaces in Clojure. You have a couple of options about how to do it however:

  • Write the code pretty much as you would in Java, just using the Java interop from Clojure to call the relevant Swing APIs. This article does a good job of explaining how, with a bit of macro magic as well to make your life easier.
  • Use a Clojure GUI wrapper for Swing, e.g. seesaw or clj-swing. My take is that these tools have the potential to help you write some really neat GUI code in idiomatic Clojure
like image 147
mikera Avatar answered Oct 21 '22 13:10

mikera


One really cool feature of Clojure's software transactional memory subsystem is that it allows you to set watches on variables: whenever the variable is changed (by anything), your callback gets executed. This lends itself to a powerful sort of GUI programming where the GUI updates itself automagically based on the state of your variables.

A short but non-trivial Swing GUI example is described in detail at http://www.paullegato.com/blog/swing-clojure-gui-black-scholes/ .

like image 27
Paul Legato Avatar answered Oct 21 '22 12:10

Paul Legato