I'm attempting to change the colour of Progress Bars in my current Swing L&F (I'm using Nimbus at the moment) by using a custom Painter object, but when created these Progress Bars sometimes stick with their original colouring (this change seems to occur randomly).
I'm probably missing something simple but I'm stumped, Painter object (and it's invocation below)...
import javax.swing.Painter;
import java.awt.*;
public class ProgressPainter implements Painter {
private Color light, dark;
private GradientPaint gradPaint;
public ProgressPainter(Color light, Color dark) {
this.light = light;
this.dark = dark;
}
@Override
public void paint(Graphics2D g, Object c, int w, int h) {
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
gradPaint = new GradientPaint((w / 2.0f), 0, light, (w / 2.0f), (h /2.0f), dark, true);
g.setPaint(gradPaint);
g.fillRect(2, 2, (w - 5), (h - 5));
Color outline = new Color(0, 85, 0);
g.setColor(outline);
g.drawRect(2, 2, (w - 5), (h - 5));
Color trans = new Color(outline.getRed(), outline.getGreen(), outline.getBlue(), 100);
g.setColor(trans);
g.drawRect(1, 1, (w - 3), (h - 3));
}
}
Invoked at application start-up with...
UIManager.put("ProgressBar[Enabled].foregroundPainter", new ProgressPainter(new Color(125, 255, 125), new Color(25, 175, 25)));
UIManager.put("ProgressBar[Enabled+Indeterminate].foregroundPainter", new ProgressPainter(new Color(125, 255, 125), new Color(25, 175, 25)));
A simple JProgressBar is then created later using...
JProgressBar progBar = new JProgressBar(0, 100);
progBar.setStringPainted(true);
progBar.setBounds(20, 10, 260, 30);
frame.add(progBar);
frame.setVisible(true);