All this time I was looking for a good solution of binding java model class to javafx UI. There are some articles about that and the most interesting, on my mind, is on Mike's Blog, but it's seams like a lot of code to write and some of the examples are not working at all. There are also some frameworks for binding through aspects, but still it wasn't the way I want it to be.
So, I think I found a nice solution, that is really easy to implement and to use.
Making interfaces
Let's make an interface for our future model. No rocket-science just getters and setters.
public interface TextModel { public void setText(String text); public String getText(); }
Creating a JavaFx model
There are some restrictions for using binding in javafx. One of them is that you can't pass a method in bind. for example:
Label { text:bind model.getText() // wont work }
For this reason we are going to create a javafx class, that implements our new interface and has a public field. It will helps us to use all the good binding stuff of javafx. After all we going to have a class that has a javafx property, that can be changed directly form java
public class JFxTextModel extends TextModel { public var text:String; public void setText(String text) { this.text = text; } public String getText() { return this.text; } }
For newbies in JavaFx, there is no implements option for implementing interfaces, we use extends
Now we can move all our business login on Java side and leave only views and model as a bridge on JavaFx side
def model:JFxTextModel = JFxTextModel{}; Label { text:bind model.text }
P.S.
It's really useful to have getters in interfaces, cause you can retrieve data on Java side, leaving javafx only for displaying and effect.Have a nice code :)