1
import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.FlowPane;
import javafx.fxml.FXMLLoader;


public class Main extends Application {

    public static Stage mainStage;
    private static Stage homeStage;
    @Override
    public void start(final Stage primaryStage) {

        Application.setUserAgentStylesheet(STYLESHEET_CASPIAN);
        splashStage(primaryStage);
        primaryStage.show();

        //mainStage();
        Task<Void> task = new Task<Void>() {

            @Override
            protected Void call() throws Exception {
                // TODO Auto-generated method stub
                for (int i = 1; i < 100; i++) {
                    try {

                        System.out.println(i);
                        if(i==99)
                        {
                            primaryStage.hide();
                            mainStage();
                            break;
                        }

                        Thread.sleep(100);

                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
                return null;

            }
        };      

        new Thread(task).start();




    }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          

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

    public void mainStage()
    {
        try {

            mainStage = new Stage(StageStyle.DECORATED);
            FlowPane root = (FlowPane)FXMLLoader.load(getClass().getResource("Sample.fxml"));
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            mainStage.setScene(scene);
            mainStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    public static void homeStage()
    {
        try {
            homeStage = new Stage(StageStyle.DECORATED);
            Parent root =  FXMLLoader.load(Main.class.getResource("Home.fxml"));
            Scene scene = new Scene(root);
            homeStage.setResizable(false);
            homeStage.setScene(scene);
            homeStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void splashStage(Stage primaryStage)
    {
        try {

            primaryStage.initStyle(StageStyle.TRANSPARENT);
            FlowPane root = (FlowPane)FXMLLoader.load(getClass().getResource("Splash.fxml"));
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);

        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

After i reaches 99, it will not open mainStage and primaryStage is not hided

functions details are as follows.
splashStage() - SplashScreen
homeStage() - Main Screen
mainStage() - LoginScreen

What should I do?

Irshad
  • 3,071
  • 5
  • 30
  • 51
Keval Pithva
  • 600
  • 2
  • 5
  • 21
  • Have you tried using Platform.runLater(() -> primaryStage......) as you have to do such things on JavaFX Application Thread aus the docs for Stage mention – Inge Feb 03 '16 at 09:20
  • yes i tried but did not work..do you have any idea to redirect main screen from splash screen ? – Keval Pithva Feb 03 '16 at 09:22
  • 1
    @Keval: Try again. I've tested it and it works: `Platform.runLater(() -> { primaryStage.hide(); mainStage(); });` If it's not working, you you haven't provided the info required to reproduce the problem. (Exception including stacktrace would be especially important, if one is thrown) – fabian Feb 03 '16 at 09:44
  • java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-3 at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:210) at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:393) at javafx.stage.Stage.(Stage.java:230) at application.Main.mainStage(Main.java:107) at application.Main$2.run(Main.java:77) – Keval Pithva Feb 03 '16 at 10:15
  • @Keval: That exception tells you exactly what we expected: you are doing things that should be done on the ui thread on a different thread. BTW: The comments section is a bad place to post much data. You can edit your question by clicking on `edit` on the left bottom of the question. IMHO exceptions are fromatted best by putting them inside a code block. – fabian Feb 03 '16 at 11:38
  • @keval as pointed out, `Platform.runLater()` is the correct way to do this, so [edit] your question to show your attempt using that. – James_D Feb 03 '16 at 11:40

1 Answers1

2

you should update UI only in javafx GUI thread.

so wrap your code in platform.runlater();

Platform.runLater(() -> { 
    primaryStage.hide();
    mainStage();
});

Care, this code need java 8!

More info here and here. Care, this code need java 8!

Community
  • 1
  • 1
  • I tried this code...Bt No any progress showing in splash screen...after i reaches 99..it will open login screen (mainStage()) – Keval Pithva Feb 04 '16 at 05:36