XtendFX is a little library making JavaFX programming in Java and Xtend a joy. Here's the typical JavaFX hello world written in Xtend :
@FXApp class HelloWorldXtend {
override void start(Stage it) {
title = "Hello World!"
scene = new Scene(new StackPane => [
children += new Button => [
text = "Say 'Hello World'"
onAction = [
println("Hello World!")
]
]
], 300, 250)
show
}
}
The annotation @FXApp will make the class extend javafx.application.Application
and adds
a main method. The operator =>
lets you pass lambda expressions (the blocks in square brackets) to an expression. With a constructor this makes for a nice builder pattern. Lambdas are also handy listener for events (see onAction
).
Note that besides @FXApp
this is just raw JavaFX + Xtend. Let's see how XtendFX can improve on that.
Writing JavaFX conformant java beans is super tedious: You need to declare two fields
and three methods per property!
With XtendFX you can use the @FXBean
annotation and get the boilerplate eliminated automatically:
/**
* A login bean written in Xtend.
*/
@FXBean class LoginBeanXtend {
String userName = ""
String password
}
XtendFX already supports readonly and lazy properties, but it's also very easy to build your own active annotation and make it tailored to your specific needs. See active annotations.
The JavaFX databinding framework is awesome but programming it with Java a real pain. XtendFX let's you replace the following Java:
// JAVA CODE!
startButton.disableProperty().bind(anim.statusProperty().isNotEqualTo(Animation.Status.STOPPED));
pauseButton.disableProperty().bind(anim.statusProperty().isNotEqualTo(Animation.Status.RUNNING));
resumeButton.disableProperty().bind(anim.statusProperty().isNotEqualTo(Animation.Status.PAUSED));
stopButton.disableProperty().bind(anim.statusProperty().isEqualTo(Animation.Status.STOPPED));
with this :
startButton.disableProperty -> (anim.statusProperty != STOPPED)
pauseButton.disableProperty -> (anim.statusProperty != RUNNING)
resumeButton.disableProperty -> (anim.statusProperty != PAUSED)
stopButton.disableProperty -> (anim.statusProperty == STOPPED)
Here's the full code the three small examples: