In this tutorial we are going to Create a simple Splash Screen in JavaFX using Intellij IDE with Source Code - Native JavaFX Project Tutorial from Scratch to Deployment.
#03 Create Splash Screen - Native JavaFX Project Tutorial from Scratch to Deployment
Main Class : Launcher.java
package com.ibouce.sms.launcher; import com.sun.javafx.application.LauncherImpl; import javafx.application.Application; import javafx.scene.Scene; import javafx.stage.Stage; public class Launcher extends Application { public static Stage primaryStage = null; public static Scene primaryScene = null; @Override public void init() { initPreloader init = new initPreloader(); init.check(); } @Override public void start(Stage primaryStage) { Launcher.primaryStage = primaryStage; } public static void main(String[] args) { LauncherImpl.launchApplication(Launcher.class, launchPreloader.class, args); } }
package com.me.myproject.launcher; import javafx.application.Preloader; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; public class LauncherPreloader extends Preloader { private Stage proloaderStage; @Override public void start(Stage primaryStage) throws Exception { this.proloaderStage = primaryStage; Parent root = FXMLLoader.load(getClass().getResource("/com/me/myproject/ressource/fxml/initPreloader.fxml")); Scene scene = new Scene(root); primaryStage.setScene(scene); primaryStage.show(); } @Override public void handleStateChangeNotification(StateChangeNotification info) { if (info.getType() == StateChangeNotification.Type.BEFORE_START) { proloaderStage.hide(); } } }
package com.me.myproject.launcher; import javafx.application.Platform; import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.stage.Stage; import java.io.IOException; import java.net.URL; import java.util.ResourceBundle; public class InitPreloader implements Initializable { public Label lblLoading; public static Label lblLoadingg; @Override public void initialize(URL location, ResourceBundle resources) { lblLoadingg=lblLoading; } public String checkFunctions(){ final String[] message = {""}; Thread t1 = new Thread(() -> { message[0] = "First Function"; Platform.runLater(() -> lblLoadingg.setText(message[0])); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } }); Thread t2 = new Thread(() -> { message[0] = "Second Function"; Platform.runLater(() -> lblLoadingg.setText(message[0])); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } }); Thread t3 = new Thread(() -> { message[0] = "Open Main Stage"; Platform.runLater(() -> lblLoadingg.setText(message[0])); Platform.runLater(new Runnable() { @Override public void run() { try { Thread.sleep(1000); Stage stage = new Stage(); Parent root = FXMLLoader.load(getClass().getResource("/com/me/myproject/ressource/fxml/main.fxml")); Scene scene = new Scene(root); stage.setScene(scene); stage.show(); } catch (IOException | InterruptedException e) { e.printStackTrace(); } } }); }); try { t1.start(); t1.join(); t2.start(); t2.join(); t3.start(); t3.join(); } catch (InterruptedException e) { e.printStackTrace(); } return message[0]; } }
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.Label?> <?import javafx.scene.control.ProgressBar?> <?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.text.Font?> <AnchorPane prefHeight="216.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.me.myproject.launcher.InitPreloader"> <children> <Label fx:id="lblLoading" layoutX="14.0" layoutY="185.0" text="loading..." AnchorPane.bottomAnchor="25.0" AnchorPane.leftAnchor="15.0" AnchorPane.rightAnchor="15.0" /> <ProgressBar layoutX="15.0" layoutY="156.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" /> <Label alignment="CENTER" layoutX="100.0" layoutY="73.0" text="Splash Screen" AnchorPane.leftAnchor="100.0" AnchorPane.rightAnchor="100.0" AnchorPane.topAnchor="73.0"> <font> <Font size="48.0" /> </font> </Label> </children> </AnchorPane>
Comments
Post a Comment