This discussion is archived
0 Replies Latest reply: Oct 15, 2012 12:37 AM by 785173 RSS

How do I keep The JProgressBar's nimbusOrange color changed by UIManager

785173 Newbie
Currently Being Moderated
I try to write a monitor system passed by swing Jtable , and use JProgressBar view the CPU'S rating .
Question: Changing dynamic the JProgressBar's nimbusOrange color in JtableCell by it's value realytime. The color changed by the cell's value , however the JProgressBar's color in JtableCell turn back where resizing the jframe .

-----------------------------
My code :
...
DefaultTableColumnModel dcm = (DefaultTableColumnModel) jTable1.getColumnModel();
dcm.getColumn(0).setCellRenderer(tcr);
...

------------------------------
public class ServerMonCellRenderer extends DefaultTableCellRenderer {

JTable jtable;
private JProgressBar progress;

public ServerMonCellRenderer(JTable jtable, int row, int column) {
}

public Component getTableCellRendererComponent(
JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int column) {
Component c = super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
if (column >= 4) {   //&& (value.getClass().getName().equals("java.lang.Double")))
int val = ((Double) value).intValue() ;
try {
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(laf.getName())) {
UIManager.setLookAndFeel(laf.getClassName());
ColorUIResource colorResource = null;
if (val>=90)
colorResource = new ColorUIResource(Color.green.darker().darker());
else if (val>=80 && val <90)
colorResource = new ColorUIResource(Color.blue.darker().darker());
else if (val>=40 && val <70)
colorResource = new ColorUIResource(Color.orange.darker().darker());
else if (val>=20 && val <40)
colorResource = new ColorUIResource(Color.yellow.darker().darker());
else if (val>=0 && val <20)
colorResource = new ColorUIResource(Color.red);//.darker().darker());
UIManager.getLookAndFeelDefaults().put("nimbusOrange", colorResource);
}
}
} catch (Exception e) {
e.printStackTrace();
}
//////////////////////////////////////////////
progress = getProgress();
progress.setMinimum(0);
progress.setMaximum(100);
progress.setValue(0);
progress.setStringPainted(true);
//////////////////////////////////////////////
progress.setValue(val);
//////////////////////////////////////////////
return progress;
} else {
this.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
return c;
}
}

private JProgressBar getProgress() {
if (progress == null) {
progress = new JProgressBar();
}
return progress;
}
}

-----------------------
Resizing the jframe ,ALL JProgressBar's color in Jtable changed to red

please help me

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points