Looking at the source code, this won't be easy.
The color is stored in the BasicProgressBarUI
class:
- In the constructor the color is retrieved from the
UIManager
using a static method. Static method means you cannot overwrite the call.
- Colors are stored as private fields, and the class only expose protected getters, no setters. No setters mean you cannot call it externally.
The ProgressBarUI
instance which is used for the JProgressBar
is derived from the UIManager
(UIManager#getUI
), which is again a static method.
That leaves us with not that many options. What I think is a viable one is to use the JProgressBar#setUI
method:
- This allows you to create your own UI instance
- This allows to override the protected getters
Major drawback for this approach is that it requires that you know up-front which Look and Feel your application will be using. For example if the application is using Metal, this would become
JProgressBar progressBar = ... ;
ProgressBarUI ui = new MetalProgressBarUI(){
/**
* The "selectionForeground" is the color of the text when it is painted
* over a filled area of the progress bar.
*/
@Override
protected Color getSelectionForeground() {
//return your custom color here
}
/**
* The "selectionBackground" is the color of the text when it is painted
* over an unfilled area of the progress bar.
*/
@Override
protected Color getSelectionBackground()
//return your custom color here
}
}
progressBar.setUI( ui );
Not 100% happy with this solution due to the major drawback of having to know the look-and-feel up-front.