Category: Clojure

  • Clojure Swing: Invoke Later

    (See my full list of Clojure Swing examples and tutorials at Clojure Swing Interop)

    When developing Swing GUIs for Clojure, understanding and using SwingUtilities/invokeLater  is crucial.

    Event Dispatch Thread

    Don’t confuse “thread” in this case with Clojure threading macros such as ->  and > . Threading here refers to concurrency.

    Swing GUI components are not safe to access concurrently. If two threads modify the same Swing component at the same time, the result is unpredictable. Java solved this problem by introducing the event dispatch thread. All modification Swing displays and component values should happen on the event dispatch thread.

    There is only one event dispatch thread. You access it by creating a Java Runnable and passing it to SwingUtilities/invokeLater . Lucky for us, all Clojure functions are Java Runnables. Here’s the general format.

    Warning

    Remember, ALL the screen rendering and modifications to your Swing components happen on the event dispatch thread. That means, if you tie up the event dispatch thread doing something time consuming, your app will appear to hang. This creates a horrible user experience. Any runnables you create should complete quickly.

    On a somewhat related note, any time a Swing component alerts you to an event or a value change, you will want to hand back that thread immediately and use a new thread to do any long tasks associated with the notifications.

  • Clojure GUI using Swing

    (See my full list of Clojure Swing examples and tutorials at Clojure Swing Interop)

    Simple Clojure Swing Example
    Simple Clojure Swing Example

    Here’s a really brief example for building a Java Swing app using Clojure.

     

  • Create a Clojure Server with React Client

    Sometimes you want a pure Clojure server with a React web client. It is best if you create a new one instead of using other people’s templates, because you can also get the newest libraries, frameworks, and security fixes at the same time. If you are determined to get going now with a Clojure / React JS server, grab the latest copy from https://github.com/TGeneDavis/clojure-server-with-reactjs-template on GitHub. (more…)

  • Clojure Swing Interop

    These days, many apps default to use of a web interface for their user interfaces. Sometimes you just want a traditional graphic user interface (GUI) with your Clojure code without the hassle of creating a full blown web app. A lot can be said for double clicking an uberjar and seeing an appealing desktop app spring to life.

    Those developing on vanilla Clojure running on the JVM have three main desktop options for GUI development: AWT, Swing, and JavaFX. AWT is considered to be an old tech with too many issues for standard development. JavaFX on the other hand is wonderful for huge projects managed by teams of dedicated developers, but a lot of work for little return when used on small projects. Swing still stands strong as the best choice for small projects that need a mature tool for GUI work.

    Here are my collection of posts explaining Java Swing interop with Clojure.

    Clojure Swing Tutorials and Posts

    Quick Clojure GUI example using Swing

    This is a barebones example for getting Swing up and going. It’s nice if you just want to get a “Hello World” with a GUI running.

     

    Event Dispatch and Invoke Later

    The first concept needed for successful Clojure Swing programming is an understanding of the event dispatch thread.

     

    Clojure Swing JFrame Introduction

    Clojure Swing JFrame example using JLabels and a BorderLayout.

     

    Clojure JButton and ActionListener Example

    Clojure Swing JButton using an ActionListener to print a simple message to the console.

     

    Full Sample Clojure Swing Temperature Conversion App

    Sometimes, you just need an actual code sample in a real app to see how to put together your own apps correctly. This Clojure Swing app demonstrated JLabel s, JTextField s, the event dispatch thread, ActionListener s, JFrame s and FlowLayout s.

     

    Clojure Message Dialogs

    Here’s how you integrate JOptionPane s into your Clojure program. You can have message dialogs, even if you don’t want a windowed application with a JFrame .

     

    File Dialogs in Clojure

    JFileChooser example to choose a file to slurp and print in Clojure.

  • Clojure Redis using Carmine

    Redis works great with Clojure when you use the Carmine API.

    Some of the features of Redis could be duplicated in your app, or are features that exist in the Clojure APIs. So, knowing when to use Clojure and when to use Redis is important. Use Redis when there is a chance that multiple servers may need access to the same messaging, session, or cached data. Also, use Redis anywhere you need an extremely fast-and-simple drop-in replacement for a database.

    Clojure Redis tutorials

    Note: These tutorials are to help get you started, but not considered final production grade code. Redis, like any database, should NEVER be exposed directly to Internet traffic.

    Clojure with a Touch of Redis

    Introduction to Redis with Clojure integrated as the client.

    Clojure Redis Get and Set

    This is basic session and caching management tutorial. Globally accessible data should go in a Redis key-value store.

    Clojure Redis Pub/Sub with Carmine

    Redis Pub/Sub for messaging between servers or namespaces. Helps prevent circle reference issues, and communications issues between namespaces and servers.

     

  • Clojure Cursive: Write, Run, Deploy

    Some quick notes on writing, running and deploying Clojure apps.

    When using Cursive for IntelliJ, a default Leiningen project is easiest for development. However, using the ‘app’ template in Leiningen sets up a default runnable app that can be packaged quickly into an uber JAR.

    I suggest doing most of the application development with a default project, and then use an ‘app’ project for the deployment.

    The default template for ‘app’ is as follows.

    To run a default app, use the lein run command from the command line. To create runnable JAR from the app template, use the command lein uberjar. You can then run the standalone JAR created in the ‘target/uberjar’ directory of the project.

    Using the REPL with App Templates

    When using the REPL in Cursive from an app template, you might have to set up the run configuration and then register the project.clj with the run configuration. Even once you have stumbled through that process, you may find that you have to change the name space to match you core.clj file’s namespace. And finally, you’ll likely need to load the -main function into the namespace. At that point you should finally be able to run (-main) from the REPL.

    That’s why I do most of my regular development from inside a default lein project instead of the app template.