Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have multiple FXML files (created in SceneBuilder), but only one controller. Does each scene load it's own copy of the controller?

I have multiple FXML files I have created in SceneBuilder and one controller file. When I edit a value (say for instance, an integer value) in one scene, and then switch scenes, that value does not seem to have been changed in the new scene.

When loading the fxml file for a scene, is my program loading a copy of the controller file just for (and used only by) that fxml file?

Really appreciate any help you guys can give answering this question.

like image 665
Aaron Avatar asked Apr 23 '13 00:04

Aaron


People also ask

Can two FXML files have the same controller?

It's possible to use the same controller class for multiple FXML files, but your code will be really hard to follow, and it's a very bad idea. (Also note that using the fx:controller attribute, you will have a different controller instance each time you call FXMLLoader.

How do controllers work in JavaFX using FXML?

JavaFX controller works based on MVC(Model-View-Controller) JavaFX MVC can be achieved by FXML (EFF-ects eXtended Markup Language). FXML is an XML based language used to develop the graphical user interfaces for JavaFX applications as in the HTML.

Where do FXML files go?

fxml files are in the same folder and package as the loading classes.

What does FXML file do?

FXML is an XML-based language that provides the structure for building a user interface separate from the application logic of your code.


1 Answers

Your controller file is a Java source file which gets compiled to a single Java class from which many Java object instances may be created.

At runtime the default fxml loader controller factory implementation will create a new controller instance (i.e. a new object), every time you invoke the fxml loader's load method.

Even if you are loading the same fxml file over and over again, the loader will create a new controller instance each time, each with it's own internal state independent of all others.

Similarly, if you load different fxml files all backed by the same controller class - each time you any fxml file, you will get a new controller instance.


Update to answer additional question on Controller data sharing

To share information between controllers using dependency injection or a separate initialization method, see:

Passing Parameters JavaFX FXML

Also, use of static class members will allow you to share information. Just don't use static in combination with @FXML, as that won't work.

There is a nice tutorial for working with multiple fxml files, which you may find helpful:

  • Part I
  • Part II

Note: it is technically possible to share a single controller among multiple FXML files

As pointed out in comments by Greg Brown:

it is possible to exercise greater control over controller instantiation using FXMLLoader#setController() and FXMLLoader#setControllerFactory().

I strongly do not recommend the following approach, which is further explained in the related answer to:

  • JavaFX multiple FXML and 1 shared controller
like image 182
jewelsea Avatar answered Jan 04 '23 04:01

jewelsea