GithubHelp home page GithubHelp logo

lvxingtu / fxrouter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from marcotrombino/fxrouter

0.0 1.0 0.0 32 KB

A simple JavaFX router to switch between application scenes

License: GNU General Public License v3.0

Java 100.00%

fxrouter's Introduction

FXRouter

release License: GPL v3

A simple JavaFX router to switch between application scenes

Example

Download

Get latest release here

Supported versions

  • 0.0.x - Support for Java 9
  • master (1.0.x) - Support for Java 8

Advantages

You can switch between your scenes from anywhere through a simple method, without worrying about annoying Stage settings.

Usage

1. Bind

Add FXRouter as project dependency and import it from its package:

  import com.github.fxrouter.FXRouter;

Connect FXRouter to your application stage: call bind() from your main class start() method (if you use IntelliJ IDEA) or similar:

FXRouter.bind(this, primaryStage);
You can optionally set application title and size (width, height):
FXRouter.bind(this, primaryStage, "MyApplication", 800, 600);

2. Set routes

Define your Application routes with a label identifier and its corresponding .fxml screen file:

FXRouter.when("login", "myloginscreen.fxml");
FXRouter.when("profile", "myprofilescreen.fxml");
// ... others
You can optionally specify the route title and size (width, height):
FXRouter.when("login", "myloginscreen.fxml", "My login screen", 1000, 500);

3. Switch

Switch routes from anywhere (controllers, services, etc):

FXRouter.goTo("login");     // switch to myloginscreen.fxml

Passing and retrieving data between routes

Your application could need to pass some data to another route and then retrieve those data:

Send data from the current scene

goTo() accepts two parameters: a route identifier and a Object:

(Multiples data could be stored on an appropriate Collection)
FXRouter.goTo("profile", "johndoe22");     // switch to myprofilescreen.fxml passing an username

Get data from the destination scene

getData() returns a Object which can be cast to appropriate data type:

String username = (String) FXRouter.getData();     // retrieve johndoe22

Example

Without FXRouter

A common JavaFX project starter:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

Using FXRouter

package sample;

import javafx.application.Application;
import javafx.stage.Stage;
import sample.FXRouter;                                 // import FXRouter

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
    
        FXRouter.bind(this, primaryStage, "Hello World", 300, 275);    // bind FXRouter
        FXRouter.when("firstPage", "sample.fxml");                     // set "firstPage" route
        FXRouter.goTo("firstPage");                                    // switch to "sample.fxml"
    }


    public static void main(String[] args) {
        launch(args);
    }
}

Switch animation

You can also set an animation type when you switch between routes:

FXRouter.setAnimationType("fade");
You can optionally specify the animation duration (ms):
FXRouter.setAnimationType("fade", 1200);

animationType

AnimationType Default duration
fade 800

fxrouter's People

Contributors

marcotrombino avatar

Watchers

James Cloos 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.