Skip to content

Instantly share code, notes, and snippets.

@lenw
Created August 24, 2010 15:44
Show Gist options
  • Save lenw/547764 to your computer and use it in GitHub Desktop.
Save lenw/547764 to your computer and use it in GitHub Desktop.
(ns swing.ui
(:import (java.awt BorderLayout Component GridLayout event.ActionEvent event.ActionListener event.KeyEvent)
(javax.swing BorderFactory JButton JFrame JLabel JPanel UIManager)))
(defmacro onClick
[obj & body]
`(.addActionListener ~obj
(proxy [ActionListener] []
(~'actionPerformed [evt#]
~@body))))
(def labelPrefix "Number of button clicks: ")
(def numClicks (ref 0))
(def panel (JPanel.))
(def button (JButton. "I'm a Swing Button"))
(def label (JLabel. (str labelPrefix "0 ")))
(defn createComponents []
(.setBorder panel (BorderFactory/createEmptyBorder 30 30 10 30))
(.setMnemonic button KeyEvent/VK_I)
(onClick button
(do
(dosync (alter numClicks + 1))
(.setText label (str labelPrefix " " @numClicks))))
(.setLabelFor label button)
(.setLayout panel (GridLayout. 0 1))
(.add panel button)
(.add panel label))
(def frame (JFrame. "SwingAppl"))
(createComponents)
(.add (.getContentPane frame) panel BorderLayout/CENTER)
;(.setDefaultCloseOperation frame JFrame/EXIT_ON_CLOSE)
(.pack frame)
(.setVisible frame true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment