change color of JProgressBar

i have a JProgressBar which I am using during a Task

is it possible to change the color of progress bar (default is orange to say green) when the process is finished,

thanks
max_dubAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bart CremersJava ArchitectCommented:
Your task already has a way to let your JProgressbar now the current progress (set the value of the JProgressbar). At the end of your task you can also let it change the color:
import javax.swing.*;
import java.awt.*;
import java.util.Random;
import java.util.concurrent.TimeUnit;
 
public class Test extends JFrame {
 
    private JProgressBar bar;
 
    public static void main(String[] args) throws Exception {
        Test test = new Test();
        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        test.pack();
        test.setLocationRelativeTo(null);
        test.setVisible(true);
 
        Task task = new Task(test);
        new Thread(task).start();
    }
 
    @Override
    protected void frameInit() {
        super.frameInit();
 
        bar = new JProgressBar(0, 100);
        bar.setForeground(Color.ORANGE);
 
        add(bar);
    }
 
    public void setProgress(int value) {
        bar.setValue(value);
    }
 
    public void setFinished() {
        bar.setForeground(Color.GREEN);
    }
}
 
class Task implements Runnable {
 
    private Test feedback;
 
    public Task(Test test) {
        this.feedback = test;
    }
 
    public void run() {
        try {
            Random rnd = new Random();
            int value = 0;
            while (value < 100) {
                TimeUnit.MILLISECONDS.sleep(rnd.nextInt(400) + 300);
                value += rnd.nextInt(25) + 5;
                value = Math.min(value, 100);
                feedback.setProgress(value);
            }
            feedback.setFinished();
        } catch (InterruptedException e) {
            Thread.interrupted();
        }
    }
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.