Change colors for JProgressBar with Nimbus?

后端 未结 3 544
一整个雨季
一整个雨季 2020-12-06 07:29

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

相关标签:
3条回答
  • 2020-12-06 08:15

    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!

    0 讨论(0)
  • 2020-12-06 08:16

    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);
    
    0 讨论(0)
  • 2020-12-06 08:30
    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();
        }
    }
    
    0 讨论(0)
提交回复
热议问题