does anyone know how to change the colors for JProgressBar when you use Nimbus LookAndFeel?
Change colors for JProgressBar with Nimbus?
possible duplicate of Setting the colors of a JProgressBar text –
Harmful
@Petar this solution doesn't fit really good to nimbus –
Spiegel
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 Painter –
Spiegel
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);
Thats new to me... per component defaults. Thanks a lot! –
Pleat
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!
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 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();
}
}
© 2022 - 2024 — McMap. All rights reserved.