Make splash screen with progress bar like Eclipse
Asked Answered
F

2

9

My main class loads configuration from a file then shows a frame. I want to make a splash screen with a progress bar like Eclipse so that the progress will increase while the file is being loaded and the splash disappears after the file is loaded. Then my main frame gets loaded.

MainClass code:

public static void main(String[] args) {
  ApplicationContext context = new ClassPathXmlApplicationContext(
    "classpath:/META-INF/spring/applicationContext.xml");
  // splash with progress load till this file is loaded
  UserDao userDao = context.getBean(UserDao.class);
  isRegistered = userDao.isRegistered();
  System.out.println("registered: " + isRegistered);
  if (isRegistered) {
    // progress finish and hide splash
    log.debug("user is registered"); // show frame1
  } else {
    // progress finish and hide splash
    log.debug("user is not registered"); // show frame2
  }
}

I don't have much experience with Swing, so please advise how to accomplish that.

UPDATE: i have found the following example, but it have little issue:

  • when the counter gets to the specified number it should stop at (300) it keeps counting for ever without stopping the timer and hiding the splash screen.

  • i want to bind the counter to the file loading, so while the file is loaded the progress gets loaded until the file gets loaded then the progress completes and the splash screen disappears.

    @SuppressWarnings("serial")
    @Component
    public class SplashScreen extends JWindow {
    
        static boolean isRegistered;
    
        static Log log = LogFactory.getLog(SplashScreen.class);
    
        private static JProgressBar progressBar = new JProgressBar();
        private static SplashScreen execute;
        private static int count;
        private static Timer timer1;
    
        public SplashScreen() {
    
            Container container = getContentPane();
            container.setLayout(null);
    
            JPanel panel = new JPanel();
            panel.setBorder(new javax.swing.border.EtchedBorder());
            panel.setBackground(new Color(255, 255, 255));
            panel.setBounds(10, 10, 348, 150);
            panel.setLayout(null);
            container.add(panel);
    
            JLabel label = new JLabel("Hello World!");
            label.setFont(new Font("Verdana", Font.BOLD, 14));
            label.setBounds(85, 25, 280, 30);
            panel.add(label);
    
            progressBar.setMaximum(50);
            progressBar.setBounds(55, 180, 250, 15);
            container.add(progressBar);
            loadProgressBar();
            setSize(370, 215);
            setLocationRelativeTo(null);
            setVisible(true);
        }
    
        public void loadProgressBar() {
            ActionListener al = new ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    count++;
                    progressBar.setValue(count);
                    if (count == 300) {
                        timer1.stop();
                        execute.setVisible(false);
                        return;
                    }
                }
            };
            timer1 = new Timer(50, al);
            timer1.start();
        }
    
        public static void main(String[] args) {
    
            execute = new SplashScreen();
    
            ApplicationContext context = new ClassPathXmlApplicationContext(
                    "classpath:/META-INF/spring/applicationContext.xml");
    
            UserDao userDao = context.getBean(UserDao.class);
    
            isRegistered = userDao.isRegistered();
    
    
            if (isRegistered) {
                 // show frame 1
            } else {
                                                        // show frame 2
    
            }
    
        }
    
    }
    
Falbala answered 9/7, 2012 at 17:21 Comment(1)
Did you check out this question? It has some good links.Orectic
H
9

when the counter gets to the specified number it should stop at (300) it keeps counting for ever without stopping the timer and hiding the splash screen.

The code below seems to work great (with the fatal flaw the counter may take longer then the file loading and vice versa):

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.ActionListener;
import javax.swing.*;

public class SplashScreen extends JWindow {

    static boolean isRegistered;
    private static JProgressBar progressBar = new JProgressBar();
    private static SplashScreen execute;
    private static int count;
    private static Timer timer1;

    public SplashScreen() {

        Container container = getContentPane();
        container.setLayout(null);

        JPanel panel = new JPanel();
        panel.setBorder(new javax.swing.border.EtchedBorder());
        panel.setBackground(new Color(255, 255, 255));
        panel.setBounds(10, 10, 348, 150);
        panel.setLayout(null);
        container.add(panel);

        JLabel label = new JLabel("Hello World!");
        label.setFont(new Font("Verdana", Font.BOLD, 14));
        label.setBounds(85, 25, 280, 30);
        panel.add(label);

        progressBar.setMaximum(50);
        progressBar.setBounds(55, 180, 250, 15);
        container.add(progressBar);
        loadProgressBar();
        setSize(370, 215);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void loadProgressBar() {
        ActionListener al = new ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                count++;

                progressBar.setValue(count);

                System.out.println(count);

                if (count == 300) {

                    createFrame();

                    execute.setVisible(false);//swapped this around with timer1.stop()

                    timer1.stop();
                }

            }

            private void createFrame() throws HeadlessException {
                JFrame frame = new JFrame();
                frame.setSize(500, 500);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        };
        timer1 = new Timer(50, al);
        timer1.start();
    }

    public static void main(String[] args) {
        execute = new SplashScreen();
    }
};

i want to bind the counter to the file loading, so while the file is loaded the progress gets loaded until the file gets loaded then the progress completes and the splash screen disappears.

You should take a look at ProgressMonitor and ProgressMonitorInputStream using a Task you may then check when the file is completely read and end the SplashScreen. see here for some great tutorial and explanation

Haler answered 15/7, 2012 at 14:26 Comment(4)
how can i get the progress bar to load slowely, because it loads very fast ? i tried to increase the count, but it still loads very fast.Falbala
@Msaleh if you are using the code I provided simple change the value of timer1 = new Timer(400, al); to something bigger. In the example the timer will now be called every 0.5secondsHaler
can you please explain the difference between the if (count == 300) and the timer1 = new Timer(50, al); what each one of them does.Falbala
@Msaleh the timer calls the method to increment the counter the if statement checks when the counter has reached a certain value then it stopsHaler
A
10

Java has a built in SplashScreen class just for this purpose. There is a tutorial on how to use it here.

Abvolt answered 9/7, 2012 at 17:26 Comment(1)
Thanks for that. Hadn't spotted the SplashScreen class in the API before you mentioned it.Wavawave
H
9

when the counter gets to the specified number it should stop at (300) it keeps counting for ever without stopping the timer and hiding the splash screen.

The code below seems to work great (with the fatal flaw the counter may take longer then the file loading and vice versa):

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.ActionListener;
import javax.swing.*;

public class SplashScreen extends JWindow {

    static boolean isRegistered;
    private static JProgressBar progressBar = new JProgressBar();
    private static SplashScreen execute;
    private static int count;
    private static Timer timer1;

    public SplashScreen() {

        Container container = getContentPane();
        container.setLayout(null);

        JPanel panel = new JPanel();
        panel.setBorder(new javax.swing.border.EtchedBorder());
        panel.setBackground(new Color(255, 255, 255));
        panel.setBounds(10, 10, 348, 150);
        panel.setLayout(null);
        container.add(panel);

        JLabel label = new JLabel("Hello World!");
        label.setFont(new Font("Verdana", Font.BOLD, 14));
        label.setBounds(85, 25, 280, 30);
        panel.add(label);

        progressBar.setMaximum(50);
        progressBar.setBounds(55, 180, 250, 15);
        container.add(progressBar);
        loadProgressBar();
        setSize(370, 215);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void loadProgressBar() {
        ActionListener al = new ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                count++;

                progressBar.setValue(count);

                System.out.println(count);

                if (count == 300) {

                    createFrame();

                    execute.setVisible(false);//swapped this around with timer1.stop()

                    timer1.stop();
                }

            }

            private void createFrame() throws HeadlessException {
                JFrame frame = new JFrame();
                frame.setSize(500, 500);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        };
        timer1 = new Timer(50, al);
        timer1.start();
    }

    public static void main(String[] args) {
        execute = new SplashScreen();
    }
};

i want to bind the counter to the file loading, so while the file is loaded the progress gets loaded until the file gets loaded then the progress completes and the splash screen disappears.

You should take a look at ProgressMonitor and ProgressMonitorInputStream using a Task you may then check when the file is completely read and end the SplashScreen. see here for some great tutorial and explanation

Haler answered 15/7, 2012 at 14:26 Comment(4)
how can i get the progress bar to load slowely, because it loads very fast ? i tried to increase the count, but it still loads very fast.Falbala
@Msaleh if you are using the code I provided simple change the value of timer1 = new Timer(400, al); to something bigger. In the example the timer will now be called every 0.5secondsHaler
can you please explain the difference between the if (count == 300) and the timer1 = new Timer(50, al); what each one of them does.Falbala
@Msaleh the timer calls the method to increment the counter the if statement checks when the counter has reached a certain value then it stopsHaler

© 2022 - 2024 — McMap. All rights reserved.