Setting the colors of a JProgressBar text
Asked Answered
B

2

8

I have these progress bars:

Sample Bars

I'm trying to set the color of the TEXT of the progress bars, without changing the background color or the color of the progress bar itself. As far as I can tell, setForeground() sets both the text and the bar color? Is there a way to do one without the other?

Bootblack answered 13/8, 2010 at 19:31 Comment(0)
B
21

Ah ha - looks like I can modify the UI:

    setUI(new BasicProgressBarUI() {
      protected Color getSelectionBackground() { return Color.black; }
      protected Color getSelectionForeground() { return Color.white; }
    });

This was a bit confusing, since the use of Foreground and Background weren't intuitive. Background is the color of the text when the bar isn't covering it, foreground is the color of the text when it is.

Here's the end result:

Colors adjusted

Bootblack answered 13/8, 2010 at 21:29 Comment(0)
P
4

By message with UIDefaults,

ProgressBar.background is the background color
ProgressBar.foreground is the cell color
ProgressBar.selectionBackground is the text color outside cells
ProgressBar.selectionForeground is the text colot INSIDE cells

UIManager.put("ProgressBar.selectionForeground", Color.black);
UIManager.put("ProgressBar.selectionBackground", Color.black);
Potshot answered 5/3, 2011 at 16:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.