JavaFX Bundle is Mini-Library that helps you pass parameters and objects between the controllers in your JavaFX application without using static parameters and other old techniques.
Follow these instructions to start using the library in your JavaFX projects.
You will need just a running machine with JDK installed and a simple IDE that let you import your JAR files.
Go under the release section and download the latest library in JAR file format, then throughout your IDE (I prefer IntelliJ IDEA) add the JAR file to the project.
Now you can have fun with your objects passed everywhere in your JavaFX Application.
To pass some parameters you need to create an object named FXBundle
, this object will be transferred between the controllers,
to create this object you have two options:
You can use the Builder Object of this FXBundle and next use this builder to populate this bundle with any parameters
// create new Builder instance.
FXBundle.Builder bundleBuilder = new FXBundle.Builder();
// build the object.
FXBundle bundle = bundleBuilder.build();
Another way to build the FXBundle Object is throughout a constructor that receives a map of type String => Object
// create a simple hash map.
Map<String, Object> map = new HashMap<>();
// create an instance with this map.
FXBundle bundle = new FXBundle(map);
You can fill out the bundle object from the map passed by the default put
function like the following:
// create a simple hash map.
Map<String, Object> map = new HashMap<>();
map.put("username", "younes");
map.put("password", "12345678abcd");
map.put("id", 12);
// create an instance with this map.
FXBundle bundle = new FXBundle(map);
Or throughout the Builder object using the putExtra
function that let you pass any sort of object like the following:
FXBundle bundle = new FXBundle.Builder()
.putExtra("username", "hi")
.putExtra("password", "12345678abcd")
.putExtra("id", 3)
.build();
In the pre-final step, you can directly pass this object in the 2nd parameter of the load
function from the FXMLLoader
like this:
FXBundle bundle = new FXBundle.Builder()
.putExtra("username", "younes")
.putExtra("id", 3)
.build();
Pane pane = FXMLLoader.load(..., bundle);
The controller that you are passing to it the bundle should implement the Initializable
interface,
at that stage, inside the initialize
method implemented by the last interface, you have to cast the second parameter which is of type ResourceBundle
into our FXBundle
, here is an example:
public void initialize(URL location, ResourceBundle resources) {
FXBundle bundle = (FXBundle) resources;
String userName = bundle.getStringExtra("username");
// do what ever you want with the received params.
}
After you got your passed bundle, you can take back your parameters with getters of the FXBundle
class.
We could make the same thing with extending another class which Called FXBundleController
in which you gonna
implement a method called initializeController
in which you will get a fresh FXBundle you just passed.
public class Controller extends FXBundleController {
public void initializeController(URL location, FXBundle bundle) {
String userName = bundle.getStringExtra("username");
// do what ever you want with the received params.
}
}
- Younes Charfaoui - Initial work - Younes Charfaoui
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE.md file for details