What is the best way to do GUIs in Clojure?

47,390

Solution 1

I will humbly suggest Seesaw.

Here's a REPL-based tutorial that assumes no Java or Swing knowledge.


Seesaw's a lot like what @tomjen suggests. Here's "Hello, World":

(use 'seesaw.core)

(-> (frame :title "Hello"
       :content "Hello, Seesaw"
       :on-close :exit)
  pack!
  show!)

and here's @Abhijith and @dsm's example, translated pretty literally:

(ns seesaw-test.core
  (:use seesaw.core))

(defn handler
  [event]
  (alert event
    (str "<html>Hello from <b>Clojure</b>. Button "
      (.getActionCommand event) " clicked.")))

(-> (frame :title "Hello Swing" :on-close :exit
           :content (button :text "Click Me" :listen [:action handler]))
  pack!
  show!)

Solution 2

Stuart Sierra recently published a series of blog posts on GUI-development with clojure (and swing). Start off here: http://stuartsierra.com/2010/01/02/first-steps-with-clojure-swing

Solution 3

If you want to do GUI programming I'd point to Temperature Converter or the ants colony.

Many things in Swing are done by sub-classing, particularly if you are creating custom components. For that there are two essential functions/macros: proxy and gen-class.

Now I understand where you are going with the more Lispy way. I don't think there's anything like that yet. I would strongly advise against trying to build a grandiose GUI-building framework a-la CLIM, but to do something more Lispy: start writing your Swing application and abstract out your common patterns with macros. When doing that you may end up with a language to write your kind of GUIs, or maybe some very generic stuff that can be shared and grow.

One thing you lose when writing the GUIs in Clojure is the use of tools like Matisse. That can be a strong pointing to write some parts in Java (the GUI) and some parts in Clojure (the logic). Which actually makes sense as in the logic you'll be able to build a language for your kind of logic using macros and I think there's more to gain there than with the GUI. Obviously, it depends on your application.

Solution 4

Nobody yet suggested it, so I will: Browser as UI platform. You could write your app in Clojure, including an HTTP server and then develop the UI using anything from HTML to hiccup, ClojureScript and any of the billions of JS libaries you need. If you wanted consistent browser behaviour and "desktop app look'n'feel" you could bundle chrome with your app.

This seems to be how Light Table is distributed.

Solution 5

From this page:

(import '(javax.swing JFrame JButton JOptionPane)) ;'
(import '(java.awt.event ActionListener))          ;'

(let [frame (JFrame. "Hello Swing")
     button (JButton. "Click Me")]
 (.addActionListener button
   (proxy [ActionListener] []
     (actionPerformed [evt]
       (JOptionPane/showMessageDialog  nil,
          (str "<html>Hello from <b>Clojure</b>. Button "
               (.getActionCommand evt) " clicked.")))))

 (.. frame getContentPane (add button))

 (doto frame
   (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
   .pack
   (.setVisible true)))

print("code sample");

And, of course, it would be worth looking at the interoperability section of clojure's website.

Share:
47,390

Related videos on Youtube

Marko
Author by

Marko

Well, ...

Updated on April 16, 2021

Comments

  • Marko
    Marko about 3 years

    What is the best way to do GUIs in Clojure?

    Is there an example of some functional Swing or SWT wrapper? Or some integration with JavaFX declarative GUI description which could be easily wrapped to s-expressions using some macrology?

    Any tutorials?

  • Marko
    Marko over 15 years
    Yeah, I know you can fall back to using Swing directly, but I was askig if there is more lispy way of doing it.
  • Jeroen Dirks
    Jeroen Dirks over 14 years
    Also note that in the more recent versions of closure you need to add a . in front of the member function calls in the todo block.
  • tomjen
    tomjen over 14 years
    Don't worry about loosing Mattisse. You can use the miglayout.com, which is powerful enough that you can do the layouts by hand.
  • Rayne
    Rayne over 14 years
    You can use Matisse with Clojure, because the code generated is just Java code which can be seamlessly accessed by Clojure. There is actually a tutorial for that somewhere...
  • Marko
    Marko over 14 years
    Looks like groovy SwingBuilder dsl translated to Clojure.
  • tomjen
    tomjen over 14 years
    @dev-er-dev That is very possible, I have never used groovy, but great minds do thing alike :)
  • Carl Smotricz
    Carl Smotricz over 14 years
    Can you provide a link or an example? I'd be interested but have no idea of how to begin.
  • Egon Willighagen
    Egon Willighagen about 14 years
    Carl, second that. A good tutorial on how to write Eclipse plugins using clojure would be rather nice.
  • mike3996
    mike3996 over 13 years
    I wonder when we get the first clojure-outputing GUI designer. Due to the homoiconicity, the generated code shouldn't even be bad!
  • Michael Bylstra
    Michael Bylstra over 11 years
    making Swing fun? They said it wasn't possible!
  • mydoghasworms
    mydoghasworms about 11 years
    There's no need to be humble - you can be very proud of this one! +1!
  • scape
    scape over 10 years
    this is great, but determining the proper port may be tricky on every client machine
  • Thomas Eding
    Thomas Eding about 10 years
    -1: There's a bug in your code. It says "Hello, Seesaw" instead of "Hello, World". </Joke>
  • Zelphir Kaltstahl
    Zelphir Kaltstahl about 8 years
    Really? Swing? Doesn't this limit the developer to the functionality Swing offers, just on a higher level of abstraction, if any? Does Clojure somehow make the applications look less outdated, or does it somehow improve all the areas in which Swing is weak? I am talking about bindings and MVC and all that "new" stuff, which Swing does now offer per se. Is it somehow fixed by language features of Clojure?
  • Zelphir Kaltstahl
    Zelphir Kaltstahl about 8 years
    This might be complex, but I still want to ask: Can you provide an example on how to get started with these pieces? Or describe which part of an application would be handled by which of those frameworks / libraries you mentioned?
  • Kenogu Labz
    Kenogu Labz almost 8 years
    He chose to make a focused abstraction over Swing specifically. Nothing wrong with that...
  • Hakanai
    Hakanai over 6 years
    Has JavaFX solved the problems of not looking very native yet? Serious question, because when I first checked it out, it was a long time ago, and it looked less correct than Swing.