On Jul 7, 4:58 pm, James Keats <[email protected]> wrote:
> For people's sense of sanity, it's not wise to try to run before you
> walk. ... But fine, people are free to be impatient and get
> frustrated and depressed if they so insist.
I must respectfully disagree. I was interested in learning Clojure,
and decided, not knowing Java, to learn both by translating a Java
ButtonDemo to Clojure. I didn't know Leningren either, but decided
to add this as well. Here's the result of a couple of days work:
(ns buttondemo.core
(:gen-class)
(:import [javax.swing AbstractButton JButton JPanel JFrame
ImageIcon]
[java.awt.event ActionEvent KeyEvent ActionListener])
(:use [clojure.contrib.swing-utils]))
(defn create-image-icon [path]
(if-let [img-url (clojure.java.io/resource path)]
(ImageIcon. img-url)
(.println System/err (str "File not found:" path))))
(defn enable-buttons [button-flags]
(doseq [[button flag] button-flags] (.setEnabled button flag)))
(defn initialize-buttons [b1 b2 b3]
(doto b1
(add-action-listener
(fn [_] (enable-buttons [[b1 false][b2 false][b3 true]])))
(.setVerticalTextPosition AbstractButton/CENTER)
(.setHorizontalTextPosition AbstractButton/LEADING)
(.setToolTipText "I can disable the middle button.")
(.setMnemonic KeyEvent/VK_D))
(doto b2
(add-action-listener (fn [_] (prn "I told you not to click
me.")))
(.setVerticalTextPosition AbstractButton/BOTTOM)
(.setHorizontalTextPosition AbstractButton/CENTER)
(.setToolTipText "Don't click me.")
(.setMnemonic KeyEvent/VK_M))
(doto b3
(add-action-listener
(fn [_] (enable-buttons [[b1 true][b2 true][b3 false]])))
(.setMnemonic KeyEvent/VK_E)
(.setToolTipText "I can enable the middle button.")
(.setEnabled false))
)
(defn -main []
(let [[b1 b2 b3 :as buttons] (for [[title file]
[["Disable middle button"
"right.gif"]
["Middle button" "middle.gif"]
["Enable middle button"
"left.gif"]]]
(JButton. title (create-image-icon
file)))
panel (doto (JPanel.) (.setOpaque true))]
(doseq [b buttons] (doto panel (.add b)))
(initialize-buttons b1 b2 b3)
(do-swing-and-wait
(doto (JFrame. "Button Demo")
(.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
(.setContentPane panel)
(.pack)
(.setVisible true))))
)
If I had waited to attempt the translation before developing some
proficiency in Java and Clojure first, I would not have bothered.
There are even improvements on the java
code; e.g., a test in the action listener in the original code is
eliminated in this version. Not needed.
Here's my Leiningren project file:
(defproject buttondemo "1.0.0-SNAPSHOT"
:description "Translation of the abysmal java button demo into
clojure."
:dependencies [[org.clojure/clojure "1.2.0"]
[org.clojure/clojure-contrib "1.2.0"]]
; :resources-path "resources/"
:main buttondemo.core
:license {:url "http://bit.ly/igfXCC"
:comments "Derived in April 2011 from the above url."}
)
The moral of this story is: don't let anyone clip your wings.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en