6

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.

Aaron
  • 63
  • 1
  • 2
  • 6

1 Answers1

16

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:


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:

Community
  • 1
  • 1
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • Thanks, jewelsea! I thought that might be the case. With there being a new instance of the controller for each loaded fxml file, is there any way to get those instances to share information? Or if I were to rewrite my program to use different controllers for each fxml file, can you get different controllers to share information? – Aaron Apr 23 '13 at 15:01
  • 1
    You rock, jewelsea! I'll take a look at those now. Thank you VERY VERY MUCH!! – Aaron Apr 23 '13 at 17:57
  • 1
    Note that it is possible to exercise greater control over controller instantiation using FXMLLoader#setController() and FXMLLoader#setControllerFactory(). However, a 1:1 relationship between a loaded document and a controller is probably the most common case. – Greg Brown May 24 '13 at 15:05
  • 2
    Also, you might consider using a pub/sub API such as EventBus (http://eventbus.org/) for inter-controller communication. Controllers can subscribe to topics they wish to be notified about and publish messages for other controllers to respond to. – Greg Brown May 24 '13 at 15:09