Kensoft PH
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
No Result
View All Result
Home Java

How to create JavaFX Splash Screen | 100% best for beginners

April 19, 2021 - Updated on September 21, 2022
in Java
Reading Time: 4 mins read
2
JavaFX Splash Screen
2.7k
VIEWS
Share on FacebookShare on TwitterShare via Email

Contents

Toggle
  • JavaFX Splash Screen Tutorial
  • Source Code of JavaFX Splash Screen
      • FX_Splash.fxml – This is the JavaFX Splash Screen window
      • FX_SplashController.java – This is the controller of your Splash.fxml
      • Main.java – This is the main class
    • JavaFX controller explanation
  • YouTube Video

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(); .

JavaFX Splash Screen
 

YouTube Video

YouTube video

Previous Post

How to set up JavaFX and Scene Builder in NetBeans IDE | 100% best for beginners

Next Post

How to create JavaFX Login application with CSS | 100% best for beginners

KENSOFT

KENSOFT

What’s up! Kent is my name. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java

Related tutorials

How to Use the JavaFX Pie Chart 100% For Beginners
Java

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
205
How to Connect to an API Using JavaFX
Java

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
215
JavaFX SQLite Database CRUD Tutorial
Java

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
586
Next Post
JavaFX login application

How to create JavaFX Login application with CSS | 100% best for beginners

Setting up JavaFX 16 with JDK 1.8

How to set up JavaFX 16 with JDK 1.8 in NetBeans IDE | 100% best for beginners

JavaFX Alert Dialogs | 100% best for beginners

JavaFX Alert Dialogs | 100% best for beginners

Comments 2

  1. Avatar of Wei Wei says:
    3 years ago

    Thank you for sharing this. Really helpful!

    Reply
    • Avatar of KENSOFT KENSOFT says:
      3 years ago

      You’re welcome, I’m glad you found it helpful.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tools

Multi-platform installer builder

Java profiler

  • Trending
  • Comments
  • Latest
MySQL database using XAMPP

How to connect Java to MySQL database using Xampp server | 100% best for beginners

October 27, 2020 - Updated on January 23, 2023
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

April 11, 2021 - Updated on July 3, 2022
JavaFX 17

How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

November 15, 2021 - Updated on December 13, 2021
hide and show password in jPasswordField

JPasswordField in Java Hide or Show Password | 100% best for beginners

April 2, 2021 - Updated on September 21, 2022
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

3DES in Java and AES in Java

How to use AES and 3DES in Java | 100% best for beginners

JavaFX Splash Screen

How to create JavaFX Splash Screen | 100% best for beginners

set up JavaFX and Scene Builder

How to set up JavaFX and Scene Builder in NetBeans IDE | 100% best for beginners

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
How to take a screenshot on PC using Kenshot

How to Take a Screenshot on PC Using Kenshot: A Full Guide

January 18, 2024 - Updated on October 6, 2024

Latest Tutorials

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024

Popular Tutorials

  • MySQL database using XAMPP

    How to connect Java to MySQL database using Xampp server | 100% best for beginners

    0 shares
    Share 0 Tweet 0
  • Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

    0 shares
    Share 0 Tweet 0
  • How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

    0 shares
    Share 0 Tweet 0
Facebook Instagram Youtube Github LinkedIn Discord
Kensoft PH

What’s up! I'm Kent. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java, which I use to create computer applications. In a company, I created applications and a website.

Categories

Website

Check the status

Privacy Policy

Terms and Condition

Sitemap

Latest Tutorials

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024

© 2024 Made With Love By KENSOFT PH

No Result
View All Result
  • Download
    • Kenshot
  • Contact
  • About
  • Java Quiz

© 2024 Made With Love By KENSOFT PH

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.