4

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);
mdhillman
  • 91
  • 1
  • 6
  • 1
    please whats sometime to stick, not able to test your code, because I'm on cell phone, not sure about your painter, not familair with this code together with Nimbus L&F – mKorbel Jul 26 '12 at 15:37
  • 2
    An [sscce](http://sscce.org/) may get better Nimbus-specific help. See also this fall-back [alternative](http://stackoverflow.com/a/8886795/230513). – trashgod Jul 26 '12 at 15:40
  • Possible duplicate of [Change colors for JProgressBar with nimbus?](http://stackoverflow.com/questions/7174420/change-colors-for-jprogressbar-with-nimbus) – trashgod Jul 26 '12 at 15:43
  • When do you apply the UI properties? Before you do any kind of painting or after the program has started? – MadProgrammer Jul 26 '12 at 20:06
  • @MadProgrammer UI Properties are set upon starting the program, before any painting is actually done. – mdhillman Jul 27 '12 at 08:11

1 Answers1

1

I had a similar program myself.

Can't remember the actual code though.

but i had to get the UI properties, overwrite the colour and reapply them.

I was using netbeans at the time, and changing the colour field from the properties was not effecting it since the Nimbus UI was set.

Try changing the ui to other ones and see if the colour is allowed to change. I'll search for the code i had used at the time in the mean time.

found it

UIDefaults defaults = UIManager.getLookAndFeelDefaults();
defaults.put("nimbusOrange", new Color(0, 0, 255));
progressBar.putClientProperty("Nimbus.Overrides.InheritDefaults", Boolean.TRUE);
progressBar.putClientProperty("Nimbus.Overrides", defaults);

where progressBar is the JProgressBar

mangusbrother
  • 3,988
  • 11
  • 51
  • 103