title: JProgressBar Changing Bar Color dynamically
body: <p>I am using <code>JProgressBar</code> component along with the Nimbus UI Defaults. The problem is that When I want to manually change each progressbar's Bar color, I use <code>BasicProgressBarUI</code> by setting <code>JProgressBar.setUI()</code> function. This makes more trouble because I would like to just change the bar color and it seems that I loose the default look of the jprogressbar (Border, backgroundcolor dissappears). </p>
  
  <p>So I can set <code>UIDefaults</code> of Nimbus ProgressBar when the code initializes. It works.</p>
  
  <p>But I want to change each progressbar's bar color dynamically.</p>
  
  <p>Is there any other way of changing Bar color of <code>JProgressBar</code>?</p>
  
  <pre><code>public class ProgressGenerator extends JFrame {
  
      protected int minValue = 0;
      protected int maxValue = 100;
      protected int counter = 0;
      protected JProgressBar progressBar;
  
      public ProgressGenerator() {
          super("JProgressBar Demo");
          setSize(300, 100);
  
          try {
              UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
          } catch (ClassNotFoundException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          } catch (InstantiationException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          } catch (IllegalAccessException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          } catch (UnsupportedLookAndFeelException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          }
  
          progressBar = new JProgressBar();
          progressBar.setMinimum(minValue);
          progressBar.setMaximum(maxValue);
          progressBar.setStringPainted(true);
  
          progressBar.setForeground(Color.GREEN);
  
          JButton start = new JButton("Start");
          start.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  Thread runner = new Thread() {
                      public void run() {
                          counter = minValue;
                          while (counter &lt;= maxValue) {
                              Runnable runme = new Runnable() {
                                  public void run() {
                                      progressBar.setValue(counter);
                                  }
                              };
                              SwingUtilities.invokeLater(runme);
                              counter++;
                              try {
                                  Thread.sleep(100);
                              } catch (Exception ex) {
                              }
                          }
                      }
                  };
                  runner.start();
              }
          });
          getContentPane().add(progressBar, BorderLayout.CENTER);
          getContentPane().add(start, BorderLayout.WEST);
          WindowListener wndCloser = new WindowAdapter() {
              public void windowClosing(WindowEvent e) {
                  System.exit(0);
              }
          };
          addWindowListener(wndCloser);
          setVisible(true);
      }
  
      public static void main(String[] args) {
          new ProgressGenerator();
      }
  }
  </code></pre>