1

I want to change Color of Progressbar from Default-Orange to Green for Nimbus UI in NetBeans.

I have added following lines

for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIDefaults defaults= UIManager.getDefaults();

                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    UIManager.getLookAndFeelDefaults().put("ProgressBar[Enabled+Finished].foregroundPainter", Color.GREEN);
                    UIManager.getLookAndFeelDefaults().put("ProgressBar[Enabled+Finished].backgroundPainter", Color.GREEN);
                    break;
                }
            }

This doesn't make any changes. Any correction to code or any other workarounds?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
MalTec
  • 1,350
  • 2
  • 14
  • 33

3 Answers3

3

Nimbus doesn't operate like other look and feels (yea for us).

Normally you should be using a ColorUIResource, but Nimbus prefers the use of Painters, for example...

enter image description here

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.Painter;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.ColorUIResource;

public class TestNimbus {

    public static void main(String[] args) {
        new TestNimbus();
    }

    public TestNimbus() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");

                    ProgressPainter painter = new ProgressPainter(Color.WHITE, Color.GREEN);
                    UIManager.getLookAndFeelDefaults().put("ProgressBar[Enabled+Finished].foregroundPainter", painter);
                    //UIManager.getLookAndFeelDefaults().put("ProgressBar[Enabled+Finished].backgroundPainter", painter);
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridwidth = GridBagConstraints.REMAINDER;

                JProgressBar pb = new JProgressBar();
                frame.add(pb, gbc);

                pb = new JProgressBar();
                pb.setValue(50);
                frame.add(pb, gbc);

                pb = new JProgressBar();
                pb.setValue(100);
                frame.add(pb, gbc);

                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ProgressPainter implements Painter {

        private Color light, dark;
        private GradientPaint gradPaint;

        public ProgressPainter(Color light, Color dark) {
            this.light = light;
            this.dark = dark;
        }

        @Override
        public void paint(Graphics2D g, Object c, int w, int h) {
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            gradPaint = new GradientPaint((w / 2.0f), 0, light, (w / 2.0f), (h / 2.0f), dark, true);
            g.setPaint(gradPaint);
            g.fillRect(2, 2, (w - 5), (h - 5));

            Color outline = new Color(0, 85, 0);
            g.setColor(outline);
            g.drawRect(2, 2, (w - 5), (h - 5));
            Color trans = new Color(outline.getRed(), outline.getGreen(), outline.getBlue(), 100);
            g.setColor(trans);
            g.drawRect(1, 1, (w - 3), (h - 3));
        }
    }
}

I stole the painter from Custom Painter on JProgressBar, so credit there

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I have implemented the same but when there is a invocation one one progress bar to another it is not working, it is waiting for me to responde(By clicking on other GUI components). Can you please throw some light if you understood my problem – Sanjit Kumar Mishra Oct 07 '16 at 07:39
2

this could help you:

private void paintJProgressBar(Color color, JProgressBar progressBar) {
    UIDefaults defaults = new UIDefaults();
    Painter painter = new MyPainter(color);
    defaults.put("ProgressBar[Enabled].foregroundPainter", painter);
    defaults.put("ProgressBar[Enabled+Finished].foregroundPainter", painter);
    progressBar.putClientProperty("Nimbus.Overrides.InheritDefaults", Boolean.TRUE);
    progressBar.putClientProperty("Nimbus.Overrides", defaults);
}

class MyPainter extends AbstractRegionPainter {

    private Color fillColor;

    public MyPainter(Color color) {
        fillColor = new Color(
                color.getRed(), color.getGreen(), color.getBlue(), 220);
    }

    @Override
    public void doPaint(Graphics2D g, JComponent c, int width,
                        int height, Object[] extendedCacheKeys) {
        g.setColor(fillColor);
        g.fillRect(3, 3, width - 6, height - 6);
    }

    @Override
    public PaintContext getPaintContext() {
        return null;
    }

}
AvB
  • 171
  • 1
  • 2
  • 12
  • I was about to have a rant over how I just said that, but realised there is a subtle difference, perhaps you'd like to highlight what those differences are ;) – MadProgrammer Jun 24 '14 at 06:58
  • Well either your approach is exactly the same as the one I posted 15 minutes before you or it's not - while on the same path, your approach does something different from mind, the OP might like to know what ;) – MadProgrammer Jun 24 '14 at 07:03
  • excuse me, I didn't see another posts, but I think there is some differences between... – AvB Jun 24 '14 at 07:06
  • Yeah, it's the reason why I didn't jump all over you and rant on about posting an exact copy of an answer posted 15 minutes earlier ;) What I'm suggesting is to highlight those differences to the OP ;) – MadProgrammer Jun 24 '14 at 07:07
  • @MadProgrammer oh, but still I can see many differences between our posts, look again :) – AvB Jun 24 '14 at 07:14
  • Yes, I know, but perhaps you'd like to highlight it to the OP, as one of the difference is very, very important – MadProgrammer Jun 24 '14 at 07:18
  • `AbstractRegionPainter` implements `Painter`, so instead of using the `interface` like I did, you've used a abstract implementation of the `Painter`, so that part is virtually the same, but there is a very important difference between how we've set it. I thought it might be nice to explain it as the OP might not see/understand the difference ;) – MadProgrammer Jun 24 '14 at 07:21
1

A quick solution for me: change the nimbusOrange color for all components, if you dont write your own Painter.

UIDefaults defaults = UIManager.getLookAndFeelDefaults();
NimbusLookAndFeel localNimbusLookAndFeel = (NimbusLookAndFeel)UIManager.getLookAndFeel();
Color derivedColor = localNimbusLookAndFeel.getDerivedColor("nimbusBase", 0.03054375F, -0.3835404F, -0.0980392F, 0, true);
defaults.put("nimbusOrange",derivedColor);
oliholz
  • 7,447
  • 2
  • 43
  • 82