Peter
Peter

Reputation: 48958

Complex GUI in clojure

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 !

Upvotes: 11

Views: 3504

Answers (3)

mikera
mikera

Reputation: 106351

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

Upvotes: 9

Marko
Marko

Reputation: 31375

Since others are mentioning swing related answers, I'll ask you one question: Is Swing a requirement. Although writing Swing code in clojure is more pleasant then in Java, it is still Swing, with all its verbosity and annoyances, especially in complex application with hard set requirements.

Have you considered web UI, where Clojure fits much more natural? Or SWT or QT Jambi, which also can be made to work using Clojure.

Upvotes: 1

Paul Legato
Paul Legato

Reputation: 1222

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/ .

Upvotes: 6

Related Questions