Dr_Benway
Dr_Benway

Reputation: 61

Writing a JavaFX project in Clojure

I'm trying to understand how to properly setup JavaFX to work with a Clojure project. By reading various sources this is what I've come up with:

This is project.clj:

(defproject cljfx "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.8.0"]]
  :resource-paths ["lib/jfxrt.jar"]
  :main ^:skip-aot cljfx.core
  :target-path "target/%s"
  :profiles {:uberjar {:aot :all}})

I don't know if I should use :resource-paths or add JavaFX to the classpath via the :dependencies vector...

This is core.clj:

I've basically translated to Clojure an example from this tutorial:

http://docs.oracle.com/javafx/2/get_started/hello_world.htm

(ns cljfx.core
  (:gen-class
    :extends javafx.application.Application)
  (:import
    [javafx.application Application]
    [javafx.stage Stage]
    [javafx.scene Scene]
    [javafx.scene.control Button]
    [javafx.scene.layout StackPane]
    [javafx.event ActionEvent EventHandler]))


(defn -main [& args]
  (Application/launch cljfx.core args))

(defn button [text]
  (doto (Button.)
    (.setText (str "Say " text))
    (.setOnAction (proxy [EventHandler] []
                    (handle [event]
                      (println text))))))

(defn -start [primaryStage]
  (let [root (doto (StackPane.)
               (-> (.getChildren)
                   (.add (button "Hello World!"))))]
    (doto primaryStage
      (.setScene (Scene. root 300 250))
      (.show))))

This doesn't compile, and I don't know what I'm doing wrong... can you help me?

Here is the error: http://pastebin.com/sYeK7MJd

Upvotes: 3

Views: 1644

Answers (1)

Jonah Benton
Jonah Benton

Reputation: 3708

There may be other problems, but the root problem in the pastebin log is:

Caused by: clojure.lang.ArityException: Wrong number of args (2) passed to: core/-start

When using gen-class and providing method implementations, every method needs to take the instance itself as the first parameter. The convention is to use "this":

(defn -start [this primaryStage] 

Try that, and ensure that local instance method calls are applied to "this".

Upvotes: 2

Related Questions