0

I have a button that search some files, so I'm trying to display the progress of this task in a progress bar with percent numbers. I'm using another thread to execute this task, but it takes long time so I'd like to show it in a progress bar.

button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Runnable r = new Runnable() {

                public void run() {
                    //Process to search for files
                }
            };
            Thread t = new Thread(r);
            t.start();
        }
    });

Please help me, I don't have experience with the JProgressBar.

slugster
  • 49,403
  • 14
  • 95
  • 145
igarcia
  • 691
  • 1
  • 9
  • 26
  • 4
    1) Read the tutorial on using JProgressBars. 2) Read the tutorial on use of SwingWorker. 3) Combine concepts and have at it. Google can help you find the appropriate oracle tutorials. I think that the SwingWorker one is called "Concurrency in Swing" so have a look for that as it is well worth your while to understand these concepts. – Hovercraft Full Of Eels Mar 11 '13 at 22:08
  • Ok, thanks, it's really good to know where to start! I'll try it! – igarcia Mar 11 '13 at 22:15
  • 1
    For [example](http://stackoverflow.com/a/4637725/230513). – trashgod Mar 11 '13 at 22:16

1 Answers1

2

After reading some tutorials to make the progress bar work, I've implemented the next solution with an indeterminated progress bar:

button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Runnable r = new Runnable() {

                public void run() {
                        progressBar.setIndeterminate(true);
                        progressBar.setVisible(true);
                        progressBar.setStringPainted(true);
                        progressBar.setString("Searching..");

                    //Process to search for files

                        progressBar.setIndeterminate(false);
                        progressBar.setString("Completed Search");

                }
            };
            Thread t = new Thread(r);
            t.start();
        }
    });
igarcia
  • 691
  • 1
  • 9
  • 26