Giter VIP home page Giter VIP logo

javafx-bundle's Introduction

JavaFX Bundle

forthebadge forthebadge

Logo Image

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.

Getting Started

Follow these instructions to start using the library in your JavaFX projects.

Prerequisites

You will need just a running machine with JDK installed and a simple IDE that let you import your JAR files.

Installing

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.

Usage

1- Create Bundle Object

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:

1- The Builder Way:

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();
1- The Map Way:

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);

2- Fill out the Bundle

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();

3- Pass the Bundle

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);

4 - Get back your passed things

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.

5- Extends the FXBundleController (Optional)

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.
    }
}

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

javafx-bundle's People

Contributors

houarizegai avatar younes-charfaoui avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.