Skip to Main Content

How to make Loading Scene before application window in javaFx?

ekocbiyikOct 20 2016 — edited Oct 21 2016

I am triying to create a loading window before real application window openning. There is a progressBar in the loading scene and it is undeterminate.

Problem is that; progressbar does not work until the real window open when i execute the program.

by the way i tried preloader class, but also not works too.

here is my code;

public class MainApp2 extends Application {

   private Stage loadingStage = new Stage();

  public static void main(String[] args) {

   launch(args);
   }

   public void start(final Stage mainStage) throws Exception {

  loadingScreen();
   appScreen();
   }

   private void loadingScreen() {

   new Task<Void>() {

   @Override
   protected Void call() throws Exception {

  ProgressBar bar = new ProgressBar(ProgressIndicator.INDETERMINATE_PROGRESS);
   bar.setPrefWidth(300);
   bar.setPrefHeight(200);
   loadingStage.initStyle(StageStyle.TRANSPARENT);
   loadingStage.initStyle(StageStyle.UNDECORATED);
   loadingStage.setScene(new Scene(bar));
   loadingStage.show();
  return null;
   }

  }.run();
   }

   private void appScreen() {

   new Task<Void>() {

   @Override
   protected Void call() throws Exception {

  Stage mainStage = new Stage();
   //get real window
   Scene root = new Scene(new MyAppWindow().getAppWindow());
   mainStage.setScene(root);
   mainStage.centerOnScreen();
   mainStage.show();
// loadingStage.close();
   return null;
   }

  }.run();
   }

   public class MyAppWindow {

   public BorderPane getAppWindow(){

  System.out.println("may be initialize take a long time...");
  for (int i = 0; i < 90000000; i++) {

  System.out.print("");
   }

   return new BorderPane(new Label("Here is real application Window!"));
   }

  }

}

public class MainApp2 extends Application {

   private static double SCREEN_WIDTH = 800;
  private static double SCREEN_HEIGHT = 600;

  private Stage loadingStage = new Stage();

  public static void main(String[] args) {

   launch(args);
   }

   public void start(final Stage mainStage) throws Exception {

  loadingScreen();
   appScreen(mainStage);
   }

  JFrame loading;
  private void loadingScreen() {

   loading = new JFrame();
   URL url = this.getClass().getResource("/images/loading.gif");
   Icon icon = new ImageIcon(url);
   JLabel label = new JLabel(icon);
   loading.setUndecorated(true);
   loading.getContentPane().add(label);
   loading.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   loading.pack();
   loading.setLocationRelativeTo(null);
   loading.setVisible(true);
   }

   private void appScreen(final Stage mainStage) {

  Platform.runLater(new Runnable() {

   public void run() {

   //get real window
   Scene root = new Scene(new AppWindow().getAppWindow());
   mainStage.setScene(root);

   mainStage.setTitle("MTA");
   mainStage.setWidth(SCREEN_WIDTH);
   mainStage.setHeight(SCREEN_HEIGHT);
   mainStage.setMinWidth(SCREEN_WIDTH);
   mainStage.setMinHeight(SCREEN_HEIGHT);
   mainStage.centerOnScreen();
   mainStage.show();

   //....
   loading.setVisible(false);
   }

  });
   }

}

Message was edited by: ekocbiyik

Comments
Post Details
Added on Oct 20 2016
3 comments
880 views