1

I create JavaFX project that includes a few controllers and different windows. I want to transfer data from one object to another controller. At the time when I get another window wants to data previously entered remain in their fields. Is the will use the database and continuous upload and download data from it is a good solution? Perhaps the creation of JSON and the object of it in each controller is the better option? Can someone say something about this? Or propose a better solution?

programmerJavaPL
  • 496
  • 6
  • 23
  • If there's a dependency, can you just give one controller a reference to the other? Or otherwise, give them both references to a "model" object and just access the needed data via that proxy. I don't see a reason to involve JSON or especially anything remote at all if you're just doing controller to controller communication in the same JFX client. – User Nov 30 '16 at 20:34

1 Answers1

0

You have some options:

  • Using a database as a middle man: It's a very bad idea in my opinion. Databases should hold the data that must be persisted and their persistence actually represents a concept from problem domain. Some temporary data that can be held in RAM are not good fits for that (even if they are going to be significant and good fits in the near future). In addition, it introduces problems like performance decrease and a constant need to check data integrity everywhere (i.e. you always have to be sure every time the data in RAM changes, the database gets updated too.)
  • Singleton pattern for storing state: you can have a singleton class that holds all your temporary data. This approach is a lot like database approach as you have some data source (as a middle man) that can be accessed from multiple points in your program, but instead of a database it's stored on RAM. So you are going to have similar problems but it's more efficient than a database and by holding references to singleton data objects, you can handle data integrity problem much easier (because when you alter your data object references you know that it's the original object that actually gets altered and you don't have to be worried about that.)

    BUT it's a very bad idea either! Using singleton pattern for storing state is an antipattern. It's not how this pattern intended to be used. Read more here: Why is Singleton considered an anti-pattern?

  • Using dependency injection frameworks like Spring: you can hold your data in spring AppicationContext with (singleton scope) and inject it wherever you want. Again this approach is essentially similar to previous approaches but it's a little bit cleaner because you're not using a static singleton class and so it may enhance testability of your application.
  • Using third-party JavaFx frameworks: There are some JavaFx frameworks that can handle problem of data sharing among many controllers. You can see some examples by reading my answer to a similar question here. Here is an example from DataFx samples which represents data sharing among two separate sender and receiver views with distinct controllers: by pressing send, the sender sends the data and receiver receives it. You can see the details in the jar or in my answer.

    enter image description here

Further reading:

Passing Parameters JavaFX FXML

Community
  • 1
  • 1
Omid
  • 5,823
  • 4
  • 41
  • 50