Javafx How to Show Scene Again
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:
- Nosotros have a method named
sendData
in the SceneAController where we gather the information. This method is called upon amouseClicked
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 amouseClicked
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:
- Save the information in an instance of the user class
- Grab the node representing the push from the
event
object - Go the instance of the stage from the node and close it
- Load the scene through the
FXMLLoader
course - Pass the information to the stage using the
setUserData
role - Create a new scene and pass information technology to the stage
- 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:
- Get the instance of the stage just like before
- Recover the object using the
getUserData
role - 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"
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:
- Remove the belongings we saw earlier from the
SceneB.fxml
file - In the SceneBController, create a new attribute of type user forth with its
set
andbecome
function
Dorsum in the sendData
function:
- Follow the kickoff three steps we saw in the previous section
- Create an
FXMLLoader
object and store in information technology the result of the statementFXMLLoader.load(...)
- Create a new instance of the SceneBController class and pass information technology the information using the
ready
method you created - Use the
setController
method in theFXMLLoader
object you created and pass it the controller instance - Use the
load
method and store its effect in theroot
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:
- Create a class and make its constructor private, and then new instances can't be created
- Create a constant of the same type as the class and initialize information technology
- 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:
- Follow the outset 4 steps of the first section
- Take hold of the static case of the UserHolder class
- Pass the information using the
set
function you created - 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 👋!
Source: https://dev.to/devtony101/javafx-3-ways-of-passing-information-between-scenes-1bm8
0 Response to "Javafx How to Show Scene Again"
แสดงความคิดเห็น