Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.