changing the color of JProgressBar

A

Aryeh M. Friedman

I have a JProgressBar that I want to be green if the underlaying
process has no errors and red if it does. I can't seem to find a way
of setting the color of the bar in the raw JProgressBar class... ideas?
 
H

HightowerC

I have a JProgressBar that I want to be green if the underlaying
process has no errors and red if it does. I can't seem to find a way
of setting the color of the bar in the raw JProgressBar class... ideas?

Try JProgressBar.setForeground(Color c)

HightowerC
 
Q

Qu0ll

I have a JProgressBar that I want to be green if the underlaying
process has no errors and red if it does. I can't seem to find a way
of setting the color of the bar in the raw JProgressBar class... ideas?

I think you'll find the colour is controlled by the prevailing PLAF.

--
And loving it,

-Q
_________________________________________________
(e-mail address removed)
(Replace the "SixFour" with numbers to email me)
 
E

Eric Sosman

Aryeh said:
No effect

Works for me.


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

class Bar extends JFrame {

public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new Bar().setVisible(true);
}
});
}

private final JProgressBar pbar = new JProgressBar(0, 10);
private final JButton butt = new JButton("Click Me");

private Bar() {
butt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
BoundedRangeModel model = pbar.getModel();
int value = model.getValue();
if (value == 3)
pbar.setForeground(Color.GREEN);
else if (value == 7)
pbar.setForeground(Color.RED);
model.setValue(value + 1);
}
});

setLayout(new FlowLayout());
add(pbar);
add(butt);
pack();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top