2

This is my existing main window of an application and I want to add splash screen to it. I've created another JFrame for splash screen. I've tried making object of splash screen JFrame and making it visible=true and also tried making static methods and using them, still not working.

        public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Splash_UI window = new Splash_UI();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
Asif Kamran Malick
  • 2,409
  • 3
  • 25
  • 47
  • See https://docs.oracle.com/en/java/javase/16/docs/api/java.desktop/java/awt/SplashScreen.html. – VGR Apr 02 '21 at 01:30
  • The Oracle tutorial, [How to Create a Splash Screen](https://docs.oracle.com/javase/tutorial/uiswing/misc/splashscreen.html) might be of some help. – Gilbert Le Blanc Apr 02 '21 at 07:52

2 Answers2

2

This is an example of the old calculator project in which I had added a splash screen this is Calculator class(Application Window) here I have made the object of Splash Class and set that visible true.

You have to remove the following 2 lines from your code

EventQueue.invokeLater(new Runnable() {
            public void run() {  

Rest I had also added a progress bar as mentioned below.

public static void main(String[] args) {
                try {
                    Splash splash = new Splash();
                    Calculator window = new Calculator();
                    splash.setVisible(true);
                    
                    for(int i = 0;i <= 100; i++) {
                        Thread.sleep(i);
                        splash.setProgressBar(i);
                        splash.setLblpercentage(Integer.toString(i)+"%");
                        if (i == 100) {
                            splash.dispose();
                                window.frame.setVisible(true);
                            }
                    }
                    
                } catch (Exception e) {
                    e.printStackTrace();
                }

Make sure to remove the main method from the JFrame of the splash screen.

Also, make components of frame static to access them from the main class and also generate getter and setters.

private static JProgressBar progressBar; 
    private static JPanel frame;
    private static JLabel lblpercentage;
    
    
    public static JLabel getLblpercentage() {
        return lblpercentage;
    }
    public static void setLblpercentage(JLabel lblpercentage) {
        Splash.lblpercentage = lblpercentage;
    }
    public static JProgressBar getProgressBar() {
        return progressBar;
    }
    public static void setProgressBar(JProgressBar progressBar) {
        Splash.progressBar = progressBar;
    }
2

Just remove this and make the object of the Splash JFrame class make it visible or use it as you want to.

 EventQueue.invokeLater(new Runnable() {
            public void run() {
                          }
                       });