Search code examples
swingmacrosclojurelisp

Expansion of vector for swing operations


I have a JPanel and a vector of JButtons and want to add each button to the panel.

The issue I'm having is that I have a variable btns that represents the vector of buttons, but the macro function just sees it as a symbol, not a vector. Is there a way to expand it somehow?

Here's the code:

(defmacro add_all [panel btns]
   ;; help?
)

(let [x 10, y 10
      btns get_buttons] ;; just a vector of JButton objects    
   (doto (JPanel.)
      (.setLayout (GridLayout. x y))
      (add-all btns)))

I would like this to expand to:

(doto (JPanel.)
     (.setLayout (GridLayout. x y))
     (.add (btns 0))
     (.add (btns 1))
     ;; etc
     (.add (btns 99)))

Solution

  • There's no need for a macro (and indeed the problem as stated cannot be solved with a macro). Just write a function which operates on a panel and a seq of buttons:

    (defn add-all [panel buttons]
      (doseq [button buttons]
        (.add panel button)))