These properties must be set before creating JProgressBar
. For example:
UIManager.put("ProgressBar.background", Color.orange);
UIManager.put("ProgressBar.foreground", Color.blue);
UIManager.put("ProgressBar.selectionBackground", Color.red);
UIManager.put("ProgressBar.selectionForeground", Color.green);
JProgressBar progress = new JProgressBar();
getContentPane().add(progress);
If you need to change the color after the component has been created or make this color change based on the percentage of progress, the solutions # can solve this problem.
Here's an example, using the properties you posted along with the question. Here's the result:
Iputinathreadjusttoshowthepercentageofthebarincreasing.
importjava.awt.*;importjavax.swing.*;publicclassWindowextendsJFrameimplementsRunnable{privatefinalJProgressBarprogress;publicWindow(Stringtitle)throwsHeadlessException{super(title);setSize(300,80);setDefaultCloseOperation(EXIT_ON_CLOSE);setLocationRelativeTo(null);UIManager.put("ProgressBar.background", Color.orange);
UIManager.put("ProgressBar.foreground", Color.blue);
UIManager.put("ProgressBar.selectionBackground", Color.red);
UIManager.put("ProgressBar.selectionForeground", Color.green);
progress = new JProgressBar();
progress.setStringPainted(true); // para mostrar a porcentagem como texto na barra
getContentPane().add(progress);
new Thread(this).start();
}
@Override
public void run() {
for(int i = 0; i < 100; i++){
progress.setValue(i);
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new Window("stackoverflow pt").setVisible(true);
});
}
}