Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure and JavaFX 2 -- passing multiple-arity arguments to JavaFX methods

A lot of JavaFx methods take var args, such as Group, which is declared in Java as:

public Group(Node... children)

Others for example:

public KeyFrame(Duration time, KeyValue... values)

I've discovered the ... means I should pass a java array to the method, so I've been doing stuff like this,

(-> timeline .getKeyFrames 
  (.addAll 
    (into-array [(KeyFrame. blabla) (KeyFrame. blabla)]))`

It's annoying to have to do (into-array...) every time there is a var arg, and it's more annoying when the var arg is a base class of what I'm actually passing in. For example

(Group. (into-array [(Circle. 100) (Rectangle. 100 100)]))

results in type mismatch error. Instead I have to do this:

(Group. (into-array Node [(Circle. 100) (Rectangle. 100 100)]))

So i made this function:

(defn make-group [& items]
  (Group. (into-array Node items)))

which is fine for Groups, but doesn't solve the general problem.

So is there a nice way of solving the ellipses/vararg problem so I don't have to make special functions for each vararg method? The requirements for this function would be that it take the method you want to call (eg Group), the fixed args, the var args, and somehow magically the function would know to find the common base class of the elements in the var args.

Thanks

like image 283
Sonicsmooth Avatar asked Oct 22 '22 08:10

Sonicsmooth


1 Answers

Have you tried putting the arguments in a vector? I haven't tried all the use cases you mention, but when using the addAll() method for example I just put the arguments into a literal vector, e.g.

    (.addAll (.getChildren btn-pane) [new-puzzle-pane check-box hint-btn
                                  check-btn solve-btn load-btn save-btn
                                  print-btn about-btn spacer])
like image 163
clartaq Avatar answered Oct 30 '22 11:10

clartaq