Javafx How to Show Scene Again

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

Miguel Manjarres

JavaFX: iii Ways of Passing Data Betwixt Scenes!

Ane of the nearly 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 fifty-fifty databases, these may not be the virtually suitable arroyo.

The goal of this tutorial is to teach y'all three dissimilar ways you tin can laissez passer information from one scene to another. Before we swoop deep on that, permit's offset make a few assumptions:

  • We have a JavaFX app with ii fxml files, SceneA and SceneB, each with its own controller, and nosotros desire to pass data from A to B.
  • We take a class that encapsulates the information we want. For the sake of this mail service, let's say this course is divers equally follows: UML User Class Diagram
  • Nosotros have a method named sendData in the SceneAController where we gather the information. This method is called upon a mouseClicked consequence
  • We have a method named receiveData in the SceneBController where we populate an instance of the user class with the information nosotros receive. This method is called upon a mouseClicked event

That'southward all 👍! Now well-nigh those methods...

Showtime Method: Using the UserData function

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 allow united states of america to adhere an arbitrary object to a node for later use. I think y'all can see how powerful is that! Although we can employ any node for this purpose, I recommend you lot use the phase itself, since information technology's the aforementioned no thing the scene.

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

  1. Save the information in an instance of the user class
  2. Grab the node representing the push from the event object
  3. Go the instance of the stage from the node and close it
  4. Load the scene through the FXMLLoader course
  5. Pass the information to the stage using the setUserData role
  6. Create a new scene and pass information technology to the stage
  7. Make the stage visible again

The sendData function should wait something similar this:

In the receiveData function the only thing we take to do is:

  1. Get the instance of the stage just like before
  2. Recover the object using the getUserData role
  3. Use the information as you see fit

Hence, the receiveData function should await something like this:

And just like that we have managed to laissez passer information between the two scenes, ¡Bravo 🎉!. This approach is very useful when we need a quick manner to send information back and forth but, what if nosotros need to laissez passer more ane object? We volition encounter how to overcome this in the next section.

2d Method: Manually Setting the Controllers

Usually when nosotros create our fxml files, the IDE automatically creates a controller for it and link them together using the post-obit belongings in the root tag of the fxml file:

            fx:controller="<bundle>.Controller"                      

Enter fullscreen mode Get out fullscreen mode

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

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

  1. Remove the belongings we saw earlier from the SceneB.fxml file
  2. In the SceneBController, create a new attribute of type user forth with its set and become function

Dorsum in the sendData function:

  1. Follow the kickoff three steps we saw in the previous section
  2. Create an FXMLLoader object and store in information technology the result of the statement FXMLLoader.load(...)
  3. Create a new instance of the SceneBController class and pass information technology the information using the ready method you created
  4. Use the setController method in the FXMLLoader object you created and pass it the controller instance
  5. Use the load method and store its effect in the root object, the rest is the same every bit before

The sendData function should await something like this:

Now in the receiveData function you can display the data stored in the user field, similar this:

This method sure is more circuitous than the last ane, but now you lot tin pass every bit much objects every bit you like betwixt the 2 scenes.

3rd Method: Using a Singleton Course

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

There are several ways we tin can implement this pattern, but the most simple one is by doing the following:

  1. Create a class and make its constructor private, and then new instances can't be created
  2. Create a constant of the same type as the class and initialize information technology
  3. Create a public static part to call back said constant

In improver to these steps, you also need to create a field of the class blazon you are interested in, in this example is user, along with its set and go functions. For the sake of this tutorial, allow's say we create a class named UserHolder and follow the steps, this would be the outcome:

Back in the sendData part:

  1. Follow the outset 4 steps of the first section
  2. Take hold of the static case of the UserHolder class
  3. Pass the information using the set function you created
  4. The rest is the same as before

And lastly in the receiveData function, you merely need to recover the object from the static instance, similar this:

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

Concluding Thoughts

In this post, nosotros learned how to laissez passer data from a scene A to a scene B in three different ways, and nosotros saw how we would implement this in our code every bit well every bit some employ case where you lot might need them. I promise you lot have constitute this useful and want to thanks for your time.

Until next time 👋!

nunnallashom.blogspot.com

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

0 Response to "Javafx How to Show Scene Again"

แสดงความคิดเห็น

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel