Search code examples
javaswingjprogressbar

Automatically start a progress bar without a button click in java


I have here a sample of progress bar:

    import java.awt.BorderLayout;
    import java.awt.Container;

    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JProgressBar;
    import javax.swing.border.Border;

    public class ProgressSample {
      public static void main(String args[]) {
        JFrame f = new JFrame("JProgressBar Sample");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container content = f.getContentPane();
        JProgressBar progressBar = new JProgressBar();
        progressBar.setValue(25);
        progressBar.setStringPainted(true);
        Border border = BorderFactory.createTitledBorder("Reading...");
        progressBar.setBorder(border);
        content.add(progressBar, BorderLayout.NORTH);
        f.setSize(300, 100);
        f.setVisible(true);
      }
    }

Now.. Is there a way to make the value run from 0 - 100% without a button triggering it. Like when I run that frame the Thread or Timer will automatically start. Is there a way to make it? Or I still need a button to trigger the timer/thread?


Solution

  • Simple answer is, yes.

    You can update the progress bar at any time, so long as you do it from within the context of the Event Dispatching Thread. What you will require is some way to tell the JProgressBar what it's new value should be, but that will depend on what it is you are trying to achieve.

    import java.awt.EventQueue;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JProgressBar;
    import javax.swing.Timer;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class AutoProgress {
    
        public static void main(String[] args) {
            new AutoProgress();
        }
    
        private JProgressBar pb;
        private int progress;
    
        public AutoProgress() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    pb = new JProgressBar();
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new GridBagLayout());
                    frame.add(pb);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
    
                    Timer timer = new Timer(50, new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            progress += 1;
                            if (progress >= 100) {
                                progress = 100;
                                ((Timer)e.getSource()).stop();
                            }
                            pb.setValue(progress);
                        }
                    });
                    timer.start();
                }
            });
        }
    
    }
    

    You might also like to have a look at JProgressBar#setIndeterminate.

    You should also have a look at How to use Swing Timers