Most of the Clojure tutorials and Clojure examples on this site require at the minimum Clojure to be installed. Usually, you want Leiningen installed too.
Easy peasy.
Seriously, do it once and ignore it for the rest of your learning experience. Here are the steps for getting Lein and Clojure up and running.
Clojure is a modern programming language. Depending on your skill level, you can create any type of software with Clojure. This includes games, web apps, mobile apps, and desktop applications.
The first step in programming Clojure is to install it and other things needed for creating Clojure programs.
JFrame s are the root windows of all modern Clojure Swing applications. In the old days, there were other options, but polite society does not speak of them.
The visible components that you use in your
JFrame are added to the content pane of the
JFrame . You request the content pane and then add your components to that content frame.
Clojure JFrame Example
Before adding components to a content pane, you need to decide one which of several layouts you will use for arranging your components.
BorderLayout is the default, so we’ll stick with that one for this simple example.
Border Layouts are divided into
NORTH ,
SOUTH ,
EAST ,
WEST , and
CENTER . We are going to use one label for each of the five locations in the content pane’s
BorderLayout . We center the text and change the colors as needed so it is easy to see the locations of each label in the
BorderLayout .
When working with UI elements in Clojure Swing, your “go to” event handler is the
ActionListener found in the
java.awt.event package. This is a one-size-fits-all handler that works for many general situations where you just need to know **something** happened.
The fist hurdle to overcome is what Clojure technique should you use to implement the
ActionListener interface? The correct answer is
reify .
Reify gives you an anonymous implementation of an interface. It is perfect for implementing Java event interfaces that are rarely named once created. Reify takes the interface name followed by method implementations as it’s arguments. The one trick that may throw off seasoned Java programmers implementing methods in Clojure’s reify, is that the first argument to functions used to implement methods is always an explicit
this .
this is usually hidden in Java implementations of methods. It’s there, just not visible.
For example, here is reify implementing an ActionListener for a typical
JButton called
my-button .
Clojure
1
2
3
(.addActionListenermy-button
(reifyActionListener
(actionPerformed[thise](println“Clicked”))))
Here’s an example of a JFrame containing a JButton that prints “Clicked” to the console when clicked.
Here is a full app demonstrating use of Swing from Clojure. Toy apps usually gloss over import code. For example, real Swing requires proper use of the event thread, and combining multiple layout types to get more complex behavior.
An example app using Clojure Swing to convert temperatures.
Note that
#(<someUIcode>) is used to pass in a function to the event dispatch thread, otherwise you have null pointer exceptions due to the code being executed BEFORE it is passed to the event dispatch thread.
Every complex GUI uses message dialogs. When using Clojure Swing interop, you will use the
JOptionPane for your message dialogs. Two keys to remember when using
JOptionPane s, is that they should always be called from within the Event Dispatch Thread (EDT). Sometimes the
JOptionPane is shown by an
ActionListener that is already on the EDT, and sometimes you need to call
invokeAndWait , because your current thread is not the EDT.
The other key point to remember with
JOptionPane s is that you can associate the message dialog with a frame, or set the frame to
nil . If a
JOptionPane ‘s frame is set to
nil , then the message dialog will center itself on the screen when it displays. If a
JFrame is provided to the
JOptionPane , then the message dialog will center itself on the
JFrame it is associated with.
Here is a basic example that provides a button to click, and that displays a plain
JOptionPane with a message. The
ActionListener is called from the EDT, so the
JOptionPane is being called from the EDT.
“Betcha always wanted a message in a JOptionPane!”))))
(.addcontent-panemy-button)
(.packmy-frame)
(.setSizemy-frame300200)
(.setVisiblemy-frametrue)))
(defn-main
“entry point for app”
[&args]
(SwingUtilities/invokeLatercreate-and-show-gui))
Here is an example that shows a
JOptionPane that displays from a normal thread. The EDT must be explicitly called with
invokeAndWait . The four most common types of message dialogs are shown.
When dealing with files, its very handy to use file dialogs. Here’s a quick example of using a
JFileChooser to select a file to
slurp and print.
Note the use of (into-array [“txt”]) as an argument to the FileNameExtensionFilter constructor. This is because the file extensions are provided as a
String... meaning a
java.lang.String array.
Also note, the
.showOpenDialog call uses
nil instead of a
JFrame . We do that because we don’t have a
JFrame showing that we want to associate the file dialog with.
Want to make advanced web pages? Try Selmer. Selmer is a powerful templating system that saves time and money during complex website development.
Selmer is a Clojure based templating system that not too surprisingly works great with Luminus framework. We’ll use Luminus to host our Selmer examples, because it is easy to get running fast.
Now you have a fully functional server with Selmer templates preconfigured. (Told you it was easy.)
cd into your project root
Clojure
1
cdmycoolapp
Run your new server.
Clojure
1
leinrun
In a web browser visit http://localhost:3000/ You should see something similar to the following.
The default Selmer template included in a Luminus project.
The web page you are looking at is a Selmer template.
Explaining Selmer
Selmer is a templating language for creating web pages. Selmer integrates with Clojure. Clojure is one of the most concise and powerful general-purpose computer programming languages available. How concise is Clojure? There are many examples of Clojure rewriting common Java and Python functions of 10-20 lines as one line of Clojure code. Selmer templates get to take advantage of Clojure when creating web pages.
For those new to templated web pages, …
The very simplest web pages are written in HTML. If all you need to do is present a restaurant menu, a short list of services, or a description of some research, simple HTML may be enough for you. More advanced and complex web pages often have information that changes based off external factors. For example, if a user of your website needs to log in to see information, a Selmer template can change the information on the page to match the information that user should see. Or if users visiting the page from a different country should see a custom price list, Selmer templates can help with displaying custom information based off regions of the visitors. Selmer templates can also leverage session information to help users of your website navigate more easily to find the information they need.
From a technical view, Selmer templates create enough of a Separation Of Concerns (SoC) that you can hire less expensive junior developers to layout your website and have more expensive senior developers handle the complex server and database code. Using Selmer templates saves your business money, because senior developers are not entangled in tasks that junior developers can handle.
To be fair, there are tons of templating systems associated with various languages and servers. All have similar benefits, except very few exploit the power of Clojure.
Taking the Guided Tour
Above, we created a web server in a Luminus Clojure project with Selmer templates. Let’s have a look around the Selmer related bits-and-pieces of our project.
I suggest opening the
mycoolapp project in Visual Code or your favorite code editor.