Skip to content

Instantly share code, notes, and snippets.

@pethaniakshay
Created June 1, 2017 05:17
Show Gist options
  • Save pethaniakshay/6b3a03f842e34b97b81a0b9102318057 to your computer and use it in GitHub Desktop.
Save pethaniakshay/6b3a03f842e34b97b81a0b9102318057 to your computer and use it in GitHub Desktop.
Splash Screen in JavaFX using FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane fx:id="ap" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="splashscreen.FXMLDocumentController">
<children>
<ImageView fitHeight="300.0" fitWidth="442.0" layoutX="116.0" layoutY="50.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@unidoc%20system_png_transparent.png" /> <!-- You should change this image file and set new image -->
</image>
</ImageView>
</children>
</AnchorPane>
package splashscreen;
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.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class FXMLDocumentController implements Initializable {
@FXML AnchorPane ap;
class ShowSplashScreen extends Thread{
@Override
public void run(){
try {
Thread.sleep(5000);
Platform.runLater(() -> {
Stage stage = new Stage();
Parent root = null;
try {
root = FXMLLoader.load(getClass().getResource("SecondScene.fxml"));
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
ap.getScene().getWindow().hide();
});
} catch (InterruptedException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
new ShowSplashScreen().start();
}
}
<?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" prefHeight="279.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60" fx:controller="splashscreen.SecondSceneController">
<children>
<Label layoutX="36.0" layoutY="124.0" prefHeight="32.0" prefWidth="529.0" text="Hello you've implemented splash screen sucessfully">
<font>
<Font size="22.0" />
</font>
</Label>
</children>
</AnchorPane>
package splashscreen;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
public class SecondSceneController implements Initializable {
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
package splashscreen;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class SplashScreen extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
@MelongNde
Copy link

It does not work

@wsulami
Copy link

wsulami commented Apr 24, 2021

Thank you very much. I spent 24 hours looking for and trying to do my splash screen then finally you made it.
and It really worked very well from the first try.
THaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaNKs

@orionnnnnnn
Copy link

Hi,Why can't my picture show?

@dalegaspi
Copy link

Nice.

you can also get away with not defining a Thread subclass by:

@Override
public void initialize(URL url, ResourceBundle rb) {
    CompletableFuture.runAsync(() -> {
        try {
            Thread.sleep(5000);
                
            Platform.runLater(() -> {
                Stage stage = new Stage();
                Parent root = null;
                try {
                    root = FXMLLoader.load(getClass().getResource("SecondScene.fxml"));
                } catch (IOException ex) {
                    Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
                }
                Scene scene = new Scene(root);
                stage.setScene(scene);
                stage.show();
                ap.getScene().getWindow().hide();
            });                
         } catch (InterruptedException ex) {
            Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
        }
    });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment