MASIGNCLEAN101

Passing From Login Screen to Main Screen in Javafx Updated FREE

Passing From Login Screen to Main Screen in Javafx

Cover image for JavaFX: 3 Ways of Passing Information Between Scenes!

Miguel Manjarres

JavaFX: 3 Means of Passing Information Between Scenes!

One of the most common issues that raises when developing a desktop awarding with JavaFX is "How can I pass this user input from scene A to scene B?", while you can always rely on text files or even databases, these may not exist the most suitable arroyo.

The goal of this tutorial is to teach you three different ways you can pass information from one scene to another. Before we dive deep on that, let'due south first make a few assumptions:

  • We have a JavaFX app with two fxml files, SceneA and SceneB, each with its ain controller, and we want to pass data from A to B.
  • Nosotros accept a class that encapsulates the data we desire. For the sake of this post, permit's say this class is divers equally follows: UML User Class Diagram
  • We have a method named sendData in the SceneAController where we gather the data. This method is called upon a mouseClicked event
  • We take a method named receiveData in the SceneBController where we populate an example of the user form with the information we receive. This method is chosen upon a mouseClicked event

That'due south all 👍! Now about those methods...

Start Method: Using the UserData office

In JavaFX, all the classes that inherit from the class Node have the following methods: setUserData(Object) and getUserData(). According to the official documentation, these methods permit usa to adhere an capricious object to a node for later utilize. I think you can see how powerful is that! Although nosotros tin can use any node for this purpose, I recommend yous utilize the stage itself, since it'southward the same no matter the scene.

In order to use this method, in the sendData function, follow these steps:

  1. Save the data in an instance of the user grade
  2. Grab the node representing the button from the upshot object
  3. Get the instance of the phase from the node and close it
  4. Load the scene through the FXMLLoader class
  5. Pass the information to the phase using the setUserData office
  6. Create a new scene and laissez passer it to the phase
  7. Make the phase visible again

The sendData part should look something similar this:

In the receiveData function the only thing nosotros have to practise is:

  1. Get the example of the phase just like before
  2. Recover the object using the getUserData role
  3. Employ the information as you see fit

Hence, the receiveData office should expect something like this:

And just like that nosotros take managed to pass information between the two scenes, ¡Bravo 🎉!. This arroyo is very useful when we need a quick way to transport data back and forth but, what if we need to laissez passer more than than one object? We will meet how to overcome this in the next section.

Second Method: Manually Setting the Controllers

Commonly when nosotros create our fxml files, the IDE automatically creates a controller for it and link them together using the following property in the root tag of the fxml file:

            fx:controller="<parcel>.Controller"                      

Enter fullscreen mode Exit fullscreen mode

In addition, this also creates a new instance of the controller class, but if we want to pass information to a field stored in the controller, like we would in a regular class, we must create the instance manually.

In order to apply this method, we need to follow these steps:

  1. Remove the property we saw earlier from the SceneB.fxml file
  2. In the SceneBController, create a new aspect of type user along with its fix and go function

Back in the sendData function:

  1. Follow the outset iii steps nosotros saw in the previous section
  2. Create an FXMLLoader object and shop in it the result of the statement FXMLLoader.load(...)
  3. Create a new instance of the SceneBController class and laissez passer information technology the information using the gear up method you created
  4. Use the setController method in the FXMLLoader object you lot created and pass it the controller instance
  5. Use the load method and store its issue in the root object, the rest is the same as before

The sendData office should wait something similar this:

At present in the receiveData part you tin can display the data stored in the user field, like this:

This method certain is more complex than the last ane, just now yous can pass as much objects every bit yous like between the two scenes.

3rd Method: Using a Singleton Class

Until now, nosotros have relied on the controllers for passing the information, just if we truly want to mimic the business logic we would do if we were using a database for instance, we can create a third class from which the ii controllers tin can access. In order to do that, the controllers must share the same case of the new grade, and we tin can accomplish that using the Singleton blueprint.

In that location are several means nosotros can implement this pattern, only the nigh simple one is past doing the post-obit:

  1. Create a class and brand its constructor individual, so new instances tin't be created
  2. Create a constant of the same type equally the grade and initialize it
  3. Create a public static function to retrieve said constant

In improver to these steps, you lot too demand to create a field of the class type yous are interested in, in this example is user, along with its set and become functions. For the sake of this tutorial, permit'south say we create a course named UserHolder and follow the steps, this would be the outcome:

Back in the sendData function:

  1. Follow the offset 4 steps of the commencement section
  2. Grab the static instance of the UserHolder class
  3. Laissez passer the information using the set function y'all created
  4. The remainder is the same as before

And lastly in the receiveData function, you just need to recover the object from the static case, like this:

And with a very few steps, we take managed to share information between the scenes through a singleton class. This approach is peculiarly useful when doing a configuration view, you can store in the singleton form the user's preferences and easily recover them anywhere in your program.

Final Thoughts

In this post, we learned how to pass information from a scene A to a scene B in three different ways, and we saw how we would implement this in our code equally well every bit some use case where yous might need them. I hope you have constitute this useful and want to thank yous for your time.

Until adjacent time 👋!

Passing From Login Screen to Main Screen in Javafx

DOWNLOAD HERE

Source: https://dev.to/devtony101/javafx-3-ways-of-passing-information-between-scenes-1bm8

Posted by: 1news2onlinea.blogspot.com

Share This :