igarcia
igarcia

Reputation: 701

Displaying the progress of a thread in percent number with a JProgressBar

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.

Upvotes: 0

Views: 2353

Answers (1)

igarcia
igarcia

Reputation: 701

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();
        }
    });

Upvotes: 2

Related Questions