Change UI Lookup for ProgressBar Swing in Nimbus Theme

后端 未结 3 1414
离开以前
离开以前 2021-01-25 10:20

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.Loo         


        
相关标签:
3条回答
  • 2021-01-25 10:53

    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

    0 讨论(0)
  • 2021-01-25 10:54

    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;
        }
    
    }
    
    0 讨论(0)
  • 2021-01-25 10:55

    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);
    
    0 讨论(0)
提交回复
热议问题