Change colors for JProgressBar with Nimbus?
Asked Answered
M

3

6

does anyone know how to change the colors for JProgressBar when you use Nimbus LookAndFeel?

Mir answered 24/8, 2011 at 11:5 Comment(4)
possible duplicate of Setting the colors of a JProgressBar textHarmful
@Petar this solution doesn't fit really good to nimbusSpiegel
Thank you so much. The two first lines of code solved it! Now I just wonder what a Painter is? Could someone please link a description or anything? I could not find documentation about it at google or sun.Mir
AbstractRegionPainter, interface PainterSpiegel
S
5

I have overridden the whole nimbusOrange-Default Value, which change all ProgressBar-Colors and any other nimbusOrange. (InternalFrame - minimize Button)
here with nimbusBase (blue)

UIDefaults defaults = UIManager.getLookAndFeelDefaults();
defaults.put("nimbusOrange",defaults.get("nimbusBase"));

Better is to write a own Painter and set this to the UIManager via

UIManager.put("ProgressBar[Enabled].backgroundPainter", myPainter);

If You want to change the Color for only a single ProgressBar instance, you can use Per-component customization

progress = new JProgressBar();
UIDefaults defaults = new UIDefaults();
defaults.put("ProgressBar[Enabled].backgroundPainter", new MyPainter());
progress.putClientProperty("Nimbus.Overrides.InheritDefaults", Boolean.TRUE);
progress.putClientProperty("Nimbus.Overrides", defaults);
Spiegel answered 24/8, 2011 at 11:10 Comment(1)
Thats new to me... per component defaults. Thanks a lot!Pleat
B
0

an example of MyPainter can be as following:

class MyPainter implements Painter<JProgressBar> {

    private final Color color;

    public MyPainter(Color c1) {
        this.color = c1;
    }
    @Override
    public void paint(Graphics2D gd, JProgressBar t, int width, int height) {
        gd.setColor(color);
        gd.fillRect(0, 0, width, height);
    }
}

but my compiler or IDE (eclipse) says that it doesn't know the Painter. is there anybody to help me!

Birdseed answered 9/12, 2014 at 12:57 Comment(1)
we must first add this import com.sun.java.swing.Painter; and if we have error do this: 1-Go to the Build Path settings in the project properties. 2-Remove the JRE System Library. 3- Add it back; Select "Add Library" and select the JRE System Library.Birdseed
K
-1
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class JProgressBarDemo extends JFrame {
    protected int minValue = 0;
    protected int maxValue = 100;
    protected int counter = 0;
    protected JProgressBar progressBar;

    public JProgressBarDemo() {
        super("JProgressBar Demo");
        setSize(300, 100);

        UIManager.put("ProgressBar.background", Color.BLACK); //colour of the background
        UIManager.put("ProgressBar.foreground", Color.RED); //colour of progress bar
        UIManager.put("ProgressBar.selectionBackground",Color.YELLOW); //colour of percentage counter on black background
        UIManager.put("ProgressBar.selectionForeground",Color.BLUE); //colour of precentage counter on red background

        progressBar = new JProgressBar();
        progressBar.setMinimum(minValue);
        progressBar.setMaximum(maxValue);
        progressBar.setStringPainted(true);
        JButton start = new JButton("Start");

        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Thread runner = new Thread() {
                    public void run() {
                        counter = minValue;
                        while (counter <= maxValue) {
                            Runnable runme = new Runnable() {
                                public void run() {
                                    progressBar.setValue(counter);
                                }
                            };

                            SwingUtilities.invokeLater(runme);
                            counter++;
                            try {
                                Thread.sleep(100);
                            } catch (Exception ex) {
                            }
                        }
                    }
                };
                runner.start();
            }
        });

        getContentPane().add(progressBar, BorderLayout.CENTER);
        getContentPane().add(start, BorderLayout.WEST);
        WindowListener wndCloser = new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        };

        addWindowListener(wndCloser);
        setVisible(true);
    }
    public static void main(String[] args) {
        new JProgressBarDemo();
    }
}
Kobe answered 21/9, 2012 at 4:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.