简体   繁体   中英

How to change the style (color) of the swing ProgressBar?

I searched the web and Stack Overflow for two hours straight and I wasn't able to find an answer to my question, I did find answers but they where not working in my case:

How to change JProgressBar color?

It seems that this solution does not work on macOS-monterey using javaSE-17 the program just want to use the default UI. The only thing I managed to edit using UiManger as recommended in this answer is the border of the progressBar.

I also tried to use the conventional method which is progressBar.setForeground(Color); it did not work, no change at all.

According to the rgb output I get on the default color for the progressBar foreground it should be red... Meaning the java program don't even use the predefined style it's like if it was using an os defined style.

只有边框样式的进度条。

Thanks for your help.

To use colors (or background color specifically), import the awt, and then swing library:

    import java.awt.*;
    import javax.swing.*;

And, set color as:

    JProgressBar b = new JProgressBar();
    b.setForeground(Color.pink);
    b.setBackground(Color.white);

I know this might not be the best way but, it works, We import awt because the Color library is in awt package. and not present in swing.

Note: Don't blame me for bad color combo!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM