You're right. Swing is the way to go, but joining all parts can be a little tough if you are learning Clojure and Swing. There are a few short examples that show how to create simple Swing GUIs in Clojure. Here is another short example that combines a simple graphical interface with a Timer object.
(ns net.dneclark.JFrameAndTimerDemo (:import (javax.swing JLabel JButton JPanel JFrame Timer)) (:gen-class)) (defn timer-action [label counter] (proxy [java.awt.event.ActionListener] [] (actionPerformed [e] (.setText label (str "Counter: " (swap! counter inc)))))) (defn timer-fn [] (let [counter (atom 0) label (JLabel. "Counter: 0") timer (Timer. 1000 (timer-action label counter)) panel (doto (JPanel.) (.add label))] (.start timer) (doto (JFrame. "Timer App") (.setContentPane panel) (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE) (.setLocation 300 300) (.setSize 200 200) (.setVisible true)))) (defn -main [] (timer-fn))
At startup, this will create a small window with a label updated every second. From your description, you will change the timer frequency from 1000 ms to 300 000 ms to activate the action every 5 minutes. To do something other than update the label, you change the contents of the timer-action function.
I think it is thread-safe, but did not check for sure. There are warnings and thread safety guides when upgrading Swing components. You probably want to check them out as well.
I hope this is informative enough to give some clues as to where to look for additional information.
EDIT . I would like to mention one more interesting thing. Note that the timer-action function changes the value of one of its arguments. The counter argument is an atom defined in timer-fn, but the action listener can change it. This is something you usually cannot do in Java. Maybe someone smarter than me can comment on whether this is a "closure". In my previous experience with languages ββlike Pascal, I would say that passing an argument is a "call by reference", as opposed to passing strict Java arguments "call-by-value". Is it something else?
EDIT 2 : after checking my facts with another question, this is actually an example of closure in Clojure.
clartaq
source share