JavaFX Splash Screen Tutorial
Most application nowadays have a splash screen when you are opening the application and many beginners wanted to learn how to make it. JavaFX splash screen, we will make a very simple splash screen in JavaFX for you to know and learn how to make a splash screen in JavaFX. The first thing we need to do is to create a new project from your NetBeans IDE. I assumed that you already knew about setting up a JavaFX.
If you don’t know how to set up JavaFX in your NetBeans IDE, you can learn from here. Once you are done creating the project, then create the JavaFX Main Class and two (2) FXML files, the first one should be your JavaFX Splash Screen with Java Controller. The second one is your main window, your dashboard, or something. You have the freedom to design your FXML file using the Scene Builder. I am going to include my source code here for your reference. Kindly see the source code below.
Source Code of JavaFX Splash Screen
Note: This is just an example. If you copy my source code and paste it into your project. You may get a bunch of errors. It is just a reference.
FX_Splash.fxml – This is the JavaFX Splash Screen window
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.Label?> <?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.text.Font?> <AnchorPane id="AnchorPane" fx:id="apane" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: linear-gradient(to left bottom, #021634, #2d1946, #581149, #7e003c, #981022);;" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/16" fx:controller="kensoft.FX_SplashController"> <children> <Label layoutX="132.0" layoutY="164.0" text="SPLASH SCREEN" textFill="WHITE"> <font> <Font size="50.0" /> </font> </Label> </children> </AnchorPane>
FX_SplashController.java – This is the controller of your Splash.fxml
package kensoft; import java.io.IOException; import java.net.URL; import java.util.ResourceBundle; import java.util.logging.Level; import java.util.logging.Logger; import javafx.application.Platform; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; import javafx.scene.Scene; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; /** * FXML Controller class * * @author KENSOFT */ public class FX_SplashController implements Initializable { /** * Initializes the controller class. */ @FXML private AnchorPane apane; private BorderPane borderPane; @Override public void initialize(URL url, ResourceBundle rb) { splash(); } private void splash() { new Thread() { @Override public void run() { try { Thread.sleep(3000); } catch (Exception e) { System.out.println(e); } Platform.runLater(new Runnable() { @Override public void run() { try { borderPane = FXMLLoader.load(getClass().getResource("FX_Dashboard.fxml")); Stage stage = new Stage(); Scene scene = new Scene(borderPane); stage.setTitle("Kensoft PH"); stage.setScene(scene); stage.setMinHeight(400); stage.setMinWidth(600); stage.show(); apane.getScene().getWindow().hide(); } catch (IOException ex) { Logger.getLogger(FX_SplashController.class.getName()).log(Level.SEVERE, null, ex); } } }); } }.start(); } }
Main.java – This is the main class
package kensoft; import java.io.IOException; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.stage.StageStyle; /** * * @author KENSOFT */ public class Main extends Application { @Override public void start(Stage stage) throws IOException { Parent pane = FXMLLoader.load(getClass().getResource("FX_Splash.fxml")); Scene scene = new Scene(pane); stage.setScene(scene); stage.initStyle(StageStyle.UNDECORATED); stage.show(); } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } }
JavaFX controller explanation
When you notice the splash() method, the logic is in there. Let’s assume that our Splash window will last for at least 3 seconds. We created a new Thread() method and override it with the run() method and we set the Thread to 3000 milliseconds for 3 seconds. The Platform.runLater() is also a requirement if we will not implement the runLater() method, we will get an error while running it. Inside the try-catch exception, that is how to show the other window and to hide the JavaFX Splash screen. Just call the object apane which is the AnchorPane layout of the FX_Splash.fxml. apane.getScene().getWindow().hide(); .
Thank you for sharing this. Really helpful!
You’re welcome, I’m glad you found it helpful.