This post is written as 16th day's post in JavaFX Advent Calendar 2012.
This post is a translated version of my 13th day's post in JavaFX Advent Calendar 2012, titled
Yesterday we read @tarchan's great post titled
Groovy's Vetoable is a one of AST transformations which is useful to GUI(thus means a Swing) developers.
When creating Swing UIs, you're often interested in monitoring the changes of value of certain UI elements. For this purpose, the usual approach is to use JavaBeans PropertyChangeListeners to be notified when the value of a class field changes. You then end up writing this very common boiler-plate code in your Java beans:
and then the result of the main method will be like as follows.
But with Groovy's
@Vetoableannotation, we can add PropertyChangeListener much easily.
@Vetoableannotation to properties which requires being scanned its change.
- Get instance.
and then result of this script will be like as follows.
(For more detail, please refer Bindable and Vetoable transformation)
@Vetoable to JavaFX
I want to apply this
@Vetoable's interesting feature to JavaFX UI.
So we are going to make a simple application with a text field and labels whose text is affected by inputs of the text field.
There are a text field and two labels.
If we input "mike" into the text field
- the left label shows the contents of the text field (i.e. "mike").
- the right label shows message from "mike" (here "Hello mike").
Designing View and Controller
With Scene Builder we are going to design the view and controller for the application.
1. Add Controller to
2. Add an event method to a
onKeyReleasedevent of the text field.
fx:ids to the text field and the labels.
4. Save it as
Reading fxml file and
JavaFX injects some objects and events into some fields and methods annotated with
@FXMLannotation in a Controller class, when reading
fxmlfile created with Scene Builder.
And the model class is here.
About Controller class
JavaFX executes the method
In this sample,
initializemethods create an instance of the
@Vetoableannotation, and then gave its
messagefield of the instance after the
namefields is changed.
The method named
keyReleased, which is called after user releases his key, changes the
namefield. And it changes the text of two labels from the
Here we run this application.
Too simple view.
Let's input Japanese character "みけ".
Labels are changed successfully as the contents of text field are input.
What is an advantage to use Groovy
It is much testable with Groovy's
@Vetoableannotation than without it.
Please look at the
initializemethod shown before.
This method provides ...
- creates an instance of
But become more simple.
These code should be in
Then the controller class becomes more simple.
We can test its behavior without starting any JavaFX application.
We were bothered by the
Threadof JavaFX, when we had thought of testing application.
But making JavaFX application with Groovy takes us more simplistic way to test JavaFX Application.
Let's use Groovy into JavaFX application!
Tomorrow we will read @rewtheblow's post.