So after having used xtend for a while as a pure templating language I thought it’s time to explore how it can be used when programming JavaFX.
JavaFX 2.x API is already designed to be used for lambda expression support which will come with Java 8 so e.g. all event handlers accept a SAM type as their sole parameter. xtend already provides lambda expressions and so one can write less code.
Java:
b.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent arg0) { // Some expression goes here } });
xtend:
b.setOnAction [ // Some expression goes here ] // or if you have an equal-sign fetish 🙂 b.onAction = [ // Some expression goes here ]
The same is true for bean properties which look like this:
Java:
Button b = new Button(); b.setText("Say hello"); String v = b.getText();
xtend:
val b = new Button(); b.text = "Say hello" val v = b.text;
The complete source code looks like this:
I’ve recorded a small video showing how to create a JavaFX project which uses xtend to the final exported application. The video highlights the amazing new feature the itemis guys added to their latest milestones when running your xtend code in debug mode – good job.
I’ll try to explore more xtend features like using the builder support but I need to first understand how this really works (too bad I missed their EclipseCon tutorial)
Pingback: Java desktop links of the week, April 16 | Jonathan Giles
Pingback: JavaFX links of the week, April 16 // JavaFX News, Demos and Insight // FX Experience
Pingback: JavaFX + xtend an ongoing love | Tomsondev Blog