Search code examples
javaclojurejavafx-2deftypegen-class

Clojure -- how to define public mutable members using deftype?


I've been trying to get http://docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm running in clojure.

I discovered that by omitting the @FXML annotation in the java version and making things public, from:

public class FXMLExampleController {
    @FXML private Text actiontarget; 
    @FXML protected void handleSubmitButtonAction(ActionEvent event) {
        actiontarget.setText("Sign in button pressed");
    }
} 

to:

public class FXMLExampleController {
    public Text actiontarget;  
    public void handleSubmitButtonAction(ActionEvent event) {
        actiontarget.setText("Sign in button pressed");
    }
}

...that it basically still works when I click the button, and fxml is able to get to the controller's public Text actiontarget, whose access is usually enabled only via the @FXML annotation.

So I'm trying to make my clojure-based controller class have public mutable fields, but in the last several hours of hunting through :gen-class and deftypes, I can't find a way to make it work. I was able to access final (default) deftype fields from a java test code, but the only online discussion I've seen says you can't have public and mutable fields, and to try :gen-class. Well, I can't find that in gen-class either, and all the gen-class examples I've been able to find use the class fields only from within clojure; I'm not sure how to define the :state in a :gen-class such that it is accessible from java, and I don't know how to make those mutable and public.

My next thing is to try clojure annotations, and then using the fx:script field rather than fx:controller to define a clojure callback... but I want to make sure it's doable/not-doable with deftype or gen-class first.

So can someone tell me if it's possible to make a java-accessible class with public mutable fields in clojure?

Thanks.


Solution

  • No, you cannot define public mutable fields in Clojure. That applies to both deftype and gen-class.

    I suppose you could try and find out whether JavaFX would be happy to call a getter and, if so, define some getFoo methods in Clojure instead.