JavaFX Tutorial - Controllers

Опубликовано: 04 Декабрь 2024
на канале: Noble Code Monkeys
955
19

I’m a big advocate of the Model-View-Controller approach to user interface development. As I have mentioned previously, I prefer layout portion of our JavaFX code to be located in the FMXL file, with a supporting controller file that helps manage states and logic flow. As your GUI applications get larger, you will want to have more than one FXML and Controller pairing, and interaction will naturally need to flow among the different components.
The focus of this tutorial will be on how to gain access to the controller object in code. There are really 2 common use cases for this.
1) Access the controller of an FXML file you have loaded in code.
2) Access the controller of an FXML file you have loaded inside another fxml file.
You can find many examples on how to do the first, but there are still a few gotchas to be aware of. As for the second approach, I find it very useful in certain circumstances, but few people talk about it.
We'll be starting from the example we discussed in the Tab Pane tutorial, so if you aren't familiar with how to load FXML files, go check that tutorial out first.
   • JavaFX Tutorial - Tab Pane and nested...