Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Build a JavaFX Application Build a Pomodoro App Properties and Bindings

Siu KWAN Yuen
PLUS
Siu KWAN Yuen
Courses Plus Student 2,898 Points

constructor of home() // the controller class

Craig has bonded the label of time in FXML to the mTextString variable in controller class. So any changes to mTextString will be shown on the label, just like Craig set a mTextString = "Hummer" at 04:43. However, the constructor has never been called (and should it only be called in the main class?), thus the mTextString should not be setted to "Hummer". Yet, the time label changed.

Can anyone tell me why the constructor of the controller class is somehow called automatically?

Siu KWAN Yuen
Siu KWAN Yuen
Courses Plus Student 2,898 Points

Things to add: Craig set the metextString = "Hummer" inside the constructor of the controller class

1 Answer

The short answer is, the Constructor gets called automatically.

jfx can do that quite easily, as long as it knows what the class is called - and we tell it by specifying 'fx:controller' in home.fxml.

That means that somewhere they do something like Home.class.newInstance() which creates the instance and then they assign our @FXML fields (like container, title) to the actual instantiated components so we can have a handle on them in the controller.