aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorscgilardi <scgilardi@gmail.com>2009-05-31 15:03:29 +0000
committerscgilardi <scgilardi@gmail.com>2009-05-31 15:03:29 +0000
commit6b85dc750d2f6781645d7249bd74592c9f73c970 (patch)
treeefae0d7420bcde1d7904e71097618dd79a3fb20d /src
parent94b791c2e921c65746c17b6c28dd546586edd3a8 (diff)
miglayout/example: remove button. Use keystrokes instead
Diffstat (limited to 'src')
-rw-r--r--src/clojure/contrib/miglayout/example.clj29
1 files changed, 16 insertions, 13 deletions
diff --git a/src/clojure/contrib/miglayout/example.clj b/src/clojure/contrib/miglayout/example.clj
index cf2d2445..2fc292ee 100644
--- a/src/clojure/contrib/miglayout/example.clj
+++ b/src/clojure/contrib/miglayout/example.clj
@@ -18,31 +18,34 @@
(:import (javax.swing JButton JFrame JLabel JPanel JTextField))
(:use (clojure.contrib
[miglayout :only (miglayout components)]
- [swing-utils :only (add-action-listener)])))
+ [swing-utils :only (add-key-typed-listener)])))
(defn fahrenheit
"Converts a Celsius temperature to Fahrenheit. Input and output are
strings. Returns \"input?\" if the input can't be parsed as a Double."
[celsius]
(try
- (format "%.2f \u00b0Fahrenheit"
- (+ 32 (* 1.8 (Double/parseDouble celsius))))
- (catch NumberFormatException _
- "input?")))
+ (format "%.2f" (+ 32 (* 1.8 (Double/parseDouble celsius))))
+ (catch NumberFormatException _ "input?")))
+
+(defn- handle-key
+ [evt in out]
+ (.setText out
+ (if (= (.getKeyChar evt) \newline)
+ (fahrenheit (.getText in))
+ "")))
(defn main
- "Lays out and displays the Temperature Converter UI"
+ "Lays out and shows a Temperature Converter UI"
[]
(let [panel
(miglayout (JPanel.)
- (JTextField.) {:id :input :width 120}
+ (JTextField. 6) {:id :input}
(JLabel. "\u00b0Celsius") :wrap
- (JButton. "Convert") {:id :convert}
- (JLabel. "\u00b0Fahrenheit") {:id :output :width 120})
- {:keys [convert input output]} (components panel)]
- (add-action-listener convert
- (fn [evt in out] (.setText out (fahrenheit (.getText in))))
- input output)
+ (JLabel.) {:id :output}
+ (JLabel. "\u00b0Fahrenheit"))
+ {:keys [input output]} (components panel)]
+ (add-key-typed-listener input handle-key input output)
(doto (JFrame. "Temperature Converter")
(.setDefaultCloseOperation JFrame/DISPOSE_ON_CLOSE)
(.add panel)