Clojure Swing

From Johns Cool Things
Jump to: navigation, search

Clojure Swing

TODO: Add a *little* bit of explanation to some examples. Maybe put things in code tags...

Stand-alone Label

(import '(javax.swing JLabel JFrame))

(def jlabel (new JLabel "Hello world"))
 
(def frame (new JFrame))
(. frame setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
(. frame add jlabel)
(. frame pack)
(. frame setVisible true)

Inline Label

(import '(javax.swing JLabel JFrame))

(def frame (new JFrame))
(. frame setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
(. frame add (new JLabel "Hello world"))
(. frame pack)
(. frame setVisible true)

Shortened with "doto"

(import '(javax.swing JLabel JFrame))

(def frame
     (doto
         (new JFrame)
       (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
       (.add (new JLabel "Hello world"))
       (.pack)
       (.setVisible true)))

Stand-alone Button

(import '(javax.swing JButton JFrame)
        '(java.awt.event ActionEvent ActionListener))

(def button (new JButton "Hello world"))
(. button addActionListener
   (proxy [ActionListener] []
     (actionPerformed [e] (println "Button Pressed"))))

(def frame
     (doto
         (new JFrame)
       (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
       (.add button)
       (.pack)
       (.setVisible true)))

Inlined Button with simple Print action

(import '(javax.swing JButton JFrame)
        '(java.awt.event ActionEvent ActionListener))
 
(def frame
     (doto
         (new JFrame)
       (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
       (.add 
        (doto
            (new JButton "Hello World")
          (.addActionListener
           (proxy [ActionListener] []
             (actionPerformed [e] (println "Button Pressed"))))))
       (.pack)
       (.setVisible true)))

Large JPanel

(import '(javax.swing JFrame JPanel)
        '(java.awt Dimension))
 
(def panel (JPanel.))
(.setPreferredSize panel (new Dimension 800 800))
 
(def frame
     (doto
         (JFrame.)
       (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
       (.add panel)
       (.pack)
       (.setVisible true)))

Panel with simple on-click event

(import '(javax.swing JButton JFrame JPanel)
        '(java.awt.event ActionEvent ActionListener MouseAdapter)
        '(java.awt Graphics Dimension Color)
        '(java.awt.image BufferedImage))
 
(def panel
     (doto
         (JPanel.)
       (.setPreferredSize (new Dimension 800 800))
       (.addMouseListener
        (proxy [MouseAdapter] []
          (mousePressed  [e] (println "Mouse clicked"))
          (mouseReleased [e] '())))))
 
(def frame
     (doto
         (JFrame.)
       (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
       (.add panel)
       (.pack)
       (.setVisible true)))

Panel with simple keyboard action

Added a simple action for pressing F2

(ns lode-stone.core)
(import '(javax.swing JButton
                      JFrame
                      JPanel
                      InputMap
                      JComponent
                      KeyStroke
                      Action
                      AbstractAction)
        '(java.awt.event ActionEvent
                         ActionListener
                         KeyEvent
                         ActionListener)
        '(java.awt Graphics Dimension Color)
        '(java.awt.image BufferedImage))

(def myAction
     (proxy [AbstractAction ActionListener] []
       (actionPerformed [e] (println "Action performed!"))))

(def panel
     (doto
         (proxy [JPanel] []
           (paint [g] nil))
       (.setPreferredSize (new Dimension 800 800))
       (.. (getInputMap) (put (KeyStroke/getKeyStroke "F2") "action"))
       (.. (getInputMap) (put (KeyStroke/getKeyStroke \a) "action"))
       (.. (getActionMap) (put "action" myAction))))
 
(def frame
     (doto
         (JFrame.)
       (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
       (.add panel)
       (.pack)
       (.setVisible true)))

(println "Press 'F2' to perform a simple action")

Added a Menu bar

Added a trivial (non-functional) menu. Also moved everything into a "main."


(ns lode-stone.core)
(import '(javax.swing JButton
                      JFrame
                      JPanel
                      InputMap
                      JComponent
                      KeyStroke
                      Action
                      AbstractAction
                      JMenuBar
                      JMenu
                      JMenuItem)
        '(java.awt.event ActionEvent
                         ActionListener
                         MouseAdapter
                         MouseMotionListener
                         MouseEvent
                         KeyEvent
                         ActionListener)
        '(java.awt Graphics Dimension Color)
        '(java.awt.image BufferedImage))


(defn main []
  (do
    (def menuBar
         (doto (JMenuBar.)
           (.add (doto (JMenu. "File")
                   (.add (JMenuItem. "New"))))))
    
    (def myAction
         (proxy [AbstractAction ActionListener] []
           (actionPerformed [e] (println "Action performed!"))))
    
    (def panel
         (doto (proxy [JPanel] []
                 (paint [g] nil))
           (.setPreferredSize (new Dimension 800 800))
           (.. (getInputMap)  (put (KeyStroke/getKeyStroke "F2") "action"))
           (.. (getInputMap)  (put (KeyStroke/getKeyStroke \a) "action"))
           (.. (getActionMap) (put "action" myAction))))
    
    (def frame
         (doto (JFrame.)
           (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
           (.setJMenuBar menuBar)
           (.add panel)
           (.pack)
           (.setVisible true)))
    (println "Press 'F2' to perform a simple action")
    )
  )

(main)


Added a simple action to the menu

(ns lode-stone.core)
(import '(javax.swing JButton
                      JFrame
                      JPanel
                      JMenuBar
                      JMenu
                      JMenuItem
                      AbstractAction)
        '(java.awt.event ActionListener)
        '(java.awt Graphics Dimension))


(defn main []
  (do
    (def menuActionListener
         (proxy [ActionListener] []
           (actionPerformed [e] (println "A menu even happened: " e))))

    (def menuBar
         (doto (JMenuBar.)
           (.add (doto (JMenu. "File")
                   (.add (doto (JMenuItem. "New")
                           (.addActionListener menuActionListener)))))))
    
    (def myAction
         (proxy [AbstractAction ActionListener] []
           (actionPerformed [e] (println "Action performed!"))))
    
    (def panel
         (doto (proxy [JPanel] []
                 (paint [g] nil))
           (.setPreferredSize (new Dimension 800 800))))
    
    (def frame
         (doto (JFrame.)
           (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
           (.setJMenuBar menuBar)
           (.add panel)
           (.pack)
           (.setVisible true)))
    )
  )

(main)