How to change JProgressBar color?
Asked Answered
L

3

18

I Used .setBackground and .setForeground and they didn't work, the color is like Orange, can't change it.

Lobule answered 27/5, 2012 at 12:28 Comment(0)
F
24

I think that these values are right for you

UIManager.put("ProgressBar.background", Color.ORANGE);
UIManager.put("ProgressBar.foreground", Color.BLUE);
UIManager.put("ProgressBar.selectionBackground", Color.RED);
UIManager.put("ProgressBar.selectionForeground", Color.GREEN);
Floeter answered 27/5, 2012 at 12:46 Comment(12)
+1 for UIManager; see also this alternative.Hesta
+1 for this answer, but i wonder how to know the value of the Key, how will I know UIManager.put(ProgressBar.whatElseCanIWriteHere, myValue) , is there any Doc, which discusses this part, If there is one, please point me at it, I be highly Thankful :-)Chaconne
@nIcE cOw there are a few www sites, but I use UIManager Defaults, because there are accesible all official L&FFloeter
@Floeter : Wow :-) thankx for pointing out. That's impressive and quite informative. Exactly what I was looking for, Thankyou so much :-)Chaconne
Shouldn't you change color only for THIS progress bar, not defaults for all progress bars in the application (like in Alex's post)?Backstairs
@Floeter if i'm using netbean ,where should i put this code.i put them in constructor ,but still progress_bar is ugly default orange colorClaar
@madhawa priyashantha depends of NativeOS and used LookAndFeel, a.m. code lines are for metal look and feelFloeter
@Floeter can i use this for Nimbus look and feel also?Claar
@madhawa priyashantha yes but you have to override Painter for JProgressBarFloeter
@Floeter finally i solved my case with your guidanceClaar
@madhawa priyashantha for excelent output to search for AbstractPainter, then is possible to create rounded corners (never used)Floeter
I'm still getting the same orange color!Mariannemariano
C
20

You should set the setStringPainted property to true:

progressBar.setStringPainted(true);
progressBar.setForeground(Color.blue);
progressBar.setString("10%");
Chive answered 10/6, 2012 at 10:20 Comment(5)
Thanks, this is the best way to set an individual color.Capablanca
But that would change the text color, not the progress color.Garlen
Just try, it changes the foreground of the progress barChive
I guess it depends on the L&F you're using. That didn't work with Nimbus for instance.Garlen
Will this be the normal Java green progress bar that we are used to seeing in Eclipse?Heall
E
0

You can use this:

UIManager.put( "nimbusOrange", new Color( 38, 139, 210 ) );

It's works for me, only change the color, you can use Color.RED or similar

Erhart answered 9/12, 2022 at 2:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.