1 year ago

#353172

test-img

Ivan Kojic

Java Swing Components not showing if Thread.sleep is called

I'm trying to make Splash screen. I have a class SplashScreen.java that extends JFrame. I saw many examples of the code I'm using but for some reason it doesn't work. After running I just get an all black JFrame without components, but in console its showing numbers from for loop one at the time as it should.

When I remove thread.sleep I get my JFrame but progressBar is at "100%" right away, as it should be.

Picture of a black JFrame without components

What am I doing wrong? Why swing components are not showing at all when I call thread.sleep()?

Code from SplashScreen.java class:

        try {
            
            Thread th = new Thread();
            for(int i = 0; i <= 100; i++) {

                lblLoadingValue.setText(i + "%");
                progressBar.setValue(i);
                if (i == 10) {
                    lblLoading.setText("Loading App...");
                    progressBar.setBackground(Color.CYAN);
                    System.out.println("1");
                }
                if (i == 20) {
                    lblLoading.setText("Loading Modules...");
                    progressBar.setBackground(Color.RED);
                    System.out.println("2");
                }
                if (i == 40) {
                    lblLoading.setText("Connecting to Database...");
                    progressBar.setBackground(Color.YELLOW);
                    System.out.println("3");
                }
                if (i == 60) {
                    lblLoading.setText("Connection Successful...");
                    progressBar.setBackground(Color.GREEN);
                    System.out.println("4");
                }
                if (i == 80) {
                    lblLoading.setText("Launching App...");
                    progressBar.setBackground(Color.ORANGE);
                    System.out.println("5");
                }
                if (i == 100) {
                    System.out.println("Done");
                    dispose();
                    Login login = new Login();
                    login.setVisible(true);
                }
                th.sleep(100);
            }
        } catch (Exception e) {
            // TODO: handle exception
        }

This code is in constructor of SplashScreen class, after creation of all swing elements. Please ignore progressBar.setBackground, I tried to see will it show any color at all..

Code from starting class(App.java):

public class App {
    
    public static void main(String[] args) {
                    
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        dbConnect conn = new dbConnect();
                        conn.connect();

//                      START SS
                        SplashScreen sp = new SplashScreen();
                        sp.setVisible(true);

                        conn.closeConnection();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }

    }

java

swing

splash-screen

java-threads

0 Answers

Your Answer

Accepted video resources