Can't change JProgressBar color in Mac OS look and feel
Asked Answered
R

1

2

I know this question has been answered before, but it's just not working for me. I followed the instructions from here: How to change JProgressBar color?

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

public class ProgressBarTest extends JFrame {

    public static void main(String args[]) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        UIManager.put("ProgressBar.background", Color.orange);
        UIManager.put("ProgressBar.foreground", Color.black);
        UIManager.put("ProgressBar.selectionBackground", Color.red);
        UIManager.put("ProgressBar.selectionForeground", Color.green);
        JProgressBar progressBar = new JProgressBar(0,100);
        progressBar.setValue(50);
        f.add(progressBar, BorderLayout.PAGE_END);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

}

All I am getting is the same old colors.

AquaProgressBarUI

I'm using Mac OS X 10.7.3 and Java 1.6. I tried the CrossPlatformLookAndFeel and it works with the new colors. However I want this in the default look and feel. How can I do this?

Rathenau answered 21/6, 2012 at 23:11 Comment(5)
1+ upvote for posting a valid sscce with formatted code on your first post here.Pleadings
But your code works fine for me. I see the requested background and foreground colors fine. I'm on a Windows 7 machine running Java 1.7.Pleadings
I am on mac OS X 10.7.3 Java 1.6 and I'm getting what you see on the screenshot. @HovercraftFullOfEels: It is my 2nd post. Thanks.Rathenau
It could be a Look and Feel issue. Consider trying another Look and Feel to see what happens. We'll have to see what other Mac users experience with your code.Pleadings
Thanks for the feedback. It looked okay for me when I set a different look and feel. But I want it in the default look-and-feel for Mac.Rathenau
G
4

To override Look & Feel defaults, make the change before constructing the GUI on the event dispatch thread, as shown below.

On the com.apple.laf.AquaLookAndFeel, the progress bar's UI delegate is an instance of com.apple.laf.AquaProgressBarUI. As you have found, it ignores many defaults in favor of the native component. If a novel color scheme is required, consider supplying your own UI delegate, as shown here.

AquaProgressBarUI:

AquaProgressBarUI

CustomProgressUI:

CustomProgressUI

ProgressBar UI Defaults:

ProgressBar.background: com.apple.laf.AquaNativeResources$CColorPaintUIResource[r=238,g=238,b=238]
ProgressBar.border: javax.swing.plaf.BorderUIResource@47f08ed8
ProgressBar.cellLength: 1
ProgressBar.cellSpacing: 0
ProgressBar.cycleTime: 3000
ProgressBar.font: sun.swing.SwingLazyValue@6446d228
ProgressBar.foreground: javax.swing.plaf.ColorUIResource[r=0,g=0,b=0]
ProgressBar.horizontalSize: javax.swing.plaf.DimensionUIResource[width=146,height=12]
ProgressBar.repaintInterval: 20
ProgressBar.selectionBackground: javax.swing.plaf.ColorUIResource[r=255,g=255,b=255]
ProgressBar.selectionForeground: javax.swing.plaf.ColorUIResource[r=0,g=0,b=0]
ProgressBar.verticalSize: javax.swing.plaf.DimensionUIResource[width=12,height=146]
ProgressBarUI: com.apple.laf.AquaProgressBarUI

SSCCE:

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

public class ProgressBarTest extends JFrame {

    public static void main(String args[]) {
        UIManager.put("ProgressBar.repaintInterval", 100);
        UIManager.put("ProgressBar.border",
            BorderFactory.createLineBorder(Color.blue, 2));
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setLayout(new GridLayout(0, 1, 5 , 5));
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.add(createBar());
                f.add(createBar());
                f.add(createBar());
                f.pack();
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            }

            private JProgressBar createBar() {
                JProgressBar progressBar = new JProgressBar(0, 100);
                progressBar.setValue(50);
                return progressBar;
            }
        });
    }
}
Gramineous answered 22/6, 2012 at 0:46 Comment(4)
So, there is no simple way to have multiple JProgressbar's with different colors but the same style of the default AquaProgressBarUI, am I correct? (I won't be able to write my own UI delegate which will look as good as the default) Thanks for the reply. I don't have enough reputation to up-vote or attach pictures directly.Rathenau
+1 hmmmm interesting lookStarwort
can't you make the progressbars adopt different L&Fs by changing the overall L&F at time of creation of the progressbar (And afterwards set it back to a default L&F you stored before)? I Think i read this somewhere here...Ozell
Some UI delegates work fine like this; some not; you'll have not try it.Gramineous

© 2022 - 2024 — McMap. All rights reserved.