Home > Software design >  How to transfer variables correctly between JavaFX stages with Spring Boot FXVeawer
How to transfer variables correctly between JavaFX stages with Spring Boot FXVeawer

Time:03-06

I'm try to use the dependecy injection in my project so i can transfer my data between scenes. In one controller i got Integer field named proj_id for example. I got this by some logic in the stage. And when i change my stage i want to get access to this variable by new stage controller. New controller have the same field Integer proj_id.

I marked them fields by @Autowired but i dont understand how suitable Bean can store value when controllers changes. I created entity Curr_proj with constructor, getters and setters, which i assume to be a Component to store variable, but still don't work.

CodePudding user response:

This isn’t a specific answer, just a general unfocused answer which might help provide an overall direction. Maybe I’ll move the info somewhere else eventually. Ignore this answer if it is not useful for you.

My advice: learn spring.

Baeldung, spring.io and other sites have enormous resources dedicated to teaching spring. Take advantage of them.

Follow modern practices using spring boot applications. Ignore any old tutorials that do not use recent spring versions or rely on xml configuration.

Also: learn JavaFX

Oracle, eden coding, makery and openjfx.io have good tutorials for learning JavaFX.

Do both those things independently. Then develop non-trivial applications of your own in both technologies independently.

Do that before you even try to integrate JavaFX and Spring. The intersection of JavaFX and Spring has far fewer high quality resources, meaning you need to research more, work harder and develop more to complete the integration.

Only after you have a good working knowledge of both technologies (not just a basic understanding), try to develop another application that uses both technologies.

Unlike other Spring tech, you can’t, at the moment, just follow some stock tutorial using a Spring Initializr generated app and expect to have a useful working app that integrates Spring and JavaFX.

IMO, the integration isn’t too hard and is worthwhile for many apps, but I wouldn’t recommend building an integrated app without comprehensive experience in both technologies (Spring and JavaFX), because they are so different, even though they are complementary.

To more specifically address your question: follow the architecture in javafx mvc, but use spring services and beans in the model layer and inject them into your controllers.

If you communicate with a server via rest use a spring rest client and data transfer objects.

If you have a local db, use spring data.

Entity classes or data transfer objects can form a part of the model in mvc or you can create separate view model classes with JavaFX properties for a mvvm architecture.

For a local db app with basic integration with jpa, see:

The overall architecture is similar to this eden SQLite JavaFX app, even though that is not spring. It demonstrates the components involved. Substitute in the spring tech, e.g. spring data, injection, spring boot app, etc for various components and wiring.

In summary, the inter communication between data in the controller is abstracted to a MVC or MVVM architecture that uses spring services and beans via dependency injection. The overall app though is more than just MVC and features other components and technologies such as:

  • spring boot to manage:

    • Spring app lifecycle
    • Spring component configuration
    • reading the app property config file
  • the JavaFX application life cycle managed by the JavaFX application class

  • The UI views in fxml controllers

  • and either

    • spring data for local db

      OR

    • spring rest to access web services on remote systems.

When auto wiring controllers, use prototype scope for the controller bean. For more info see:

Currently JavaFX 17 is modular but spring boot 2 is not. This makes integration more difficult.

For many developers the integration of JavaFX and Spring will currently be beyond their comfort zone as developers.

Future possibilities

In the future spring boot 3 will be modular.

Hopefully, also a starter dependency can be added to Spring Initializr to make it easy to generate a new JavaFX spring project.

Then combine this with good modular and plugin build support in Maven and gradle for packaging modular apps through jlink and jpackage.

Also add good IDE new project wizard support to generate a new project using both frameworks.

Also provide good tutorials for the Spring and JavaFX integration using the new technologies.

The combination of all of the above would hopefully make the integration and deployment of the technologies much more accessible.

CodePudding user response:

Okay, i really thankful for your answer, but my JavaFX knowledge is not that bad as you think it is. I agree, i must work harder and i do learn Spring right now.

I do a liitle reserch and transferring variable between scenes is quite easy in my situation, because i use FXVeawer so my controllers already are a Spring Components.

So, in my FirstController i just have to add a getter to my proj_ij field:

public Integer proj_id;

public void SomeMethod() {
proj_id = 5;
}
Integer getProj_id() {
    return proj_id;
}

That being done, i just created a field in my SecondController public FitstController FirstController and simple constructor injcection do the trick:

@Autowired
public void get_projid(FirstController FirstController) {
    this.FirstController = FirstController;
}

Now, i can access my proj_id value by typing FirstController.getProj_id in my Second Controller

  • Related