Regular method calls within clojure doto

I configure swing UI in clojure and have a block like:

  (doto main-frame
    (.setUndecorated true)
    (.setExtendedState Frame/MAXIMIZED_BOTH)
    (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
    (.setVisible true)
    )

But now I want to call

(.setBackground (.getContentPane main-frame) Color/BLACK)

before I set the visible frame, is there a better way to do this than end doto and use the syntax (.instanceMember instance args *)?

+3
source share
1 answer

If you really need one doto, then perhaps this will do:

(doto main-frame
  (.setUndecorated true)
  (.setExtendedState Frame/MAXIMIZED_BOTH)
  (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
  (-> (.getContentPane) (.setBackground Color/BLACK))
  (.setVisible true))

The above relies on being dotonot limited to Java methods, it simply inserts its first argument (evaluated) as the first argument of each next form.

doto, , , . , , set-background-on-content-pane (, , main-frame), doto:

(defn set-bg-on-frame [fr color] (.setBackground (.getContentPane fr) color))

(doto main-frame
   .
   .
   .
   (set-bg-on-frame Color/BLACK)
   (.setVisible true))
+5