1 Reply Latest reply: Dec 11, 2012 3:10 AM by jduprez RSS

    Set progressbar's color by value

    785173
      I created a few progressbars and wanted to change they color by their value . but them only view a color.
      My code :

      /*
      * To change this template, choose Tools | Templates
      * and open the template in the editor.
      */
      package tool.os;

      import java.awt.Color;
      import java.awt.Font;
      import java.awt.Graphics;
      import java.awt.Graphics2D;
      import java.awt.GridBagConstraints;
      import java.awt.GridBagLayout;
      import java.awt.Insets;
      import java.awt.Rectangle;
      import java.awt.geom.Line2D;
      import java.awt.geom.Rectangle2D;
      import java.awt.image.BufferedImage;
      import javax.swing.JButton;
      import javax.swing.JProgressBar;
      import javax.swing.border.SoftBevelBorder;

      public class HostInfo extends javax.swing.JPanel {
      ProBarColor jpr1 = new ProBarColor();
      ProBarColor jpr2 = new ProBarColor();

      /**
      * Creates new form HostInfo
      */
      public HostInfo() {
      // MemoryMonitor demo = new MemoryMonitor();
      // MemoryMonitor demo2 = new MemoryMonitor();

      jpr1.setStringPainted(true);
      jpr1.setString("C use rating");
      jpr2.setStringPainted(true);
      jpr2.setString("D use rating");

      GridBagLayout gbly = new GridBagLayout();
      GridBagConstraints gcons = new GridBagConstraints();
      gcons.insets = new Insets(20, 20, 20, 20);
      setLayout(gbly);
      setBorder(new SoftBevelBorder(5));
      // gcons.weightx = 1;
      // gcons.weighty = 4;
      // gcons.fill = GridBagConstraints.BOTH;
      // add(demo, gcons);
      // demo.surf.start();

      // gcons.weightx = 1;
      // gcons.weighty = 4;
      // gcons.fill = GridBagConstraints.BOTH;
      // gcons.insets = new Insets(20, 0, 20, 20);
      // gcons.gridwidth = GridBagConstraints.REMAINDER;
      // add(demo2, gcons);
      // demo2.surf.start();

      gcons.weightx = 1;
      gcons.weighty = 2;
      gcons.fill = GridBagConstraints.BOTH;
      gcons.insets = new Insets(0, 20, 10, 20);
      gcons.gridwidth = GridBagConstraints.REMAINDER;
      add(jpr1, gcons);
      jpr1.setValue(90);

      gcons.weightx = 1;
      gcons.weighty = 2;
      gcons.fill = GridBagConstraints.BOTH;
      gcons.insets = new Insets(0, 20, 10, 20);
      gcons.gridwidth = GridBagConstraints.REMAINDER;
      add(jpr2, gcons);
      jpr2.setValue(10);
      }
      public void setval(int val){
      jpr2.setValue(val);
      jpr2.setColor();
      }
      /**
      * This method is called from within the constructor to initialize the form.
      * WARNING: Do NOT modify this code. The content of this method is always
      * regenerated by the Form Editor.
      */
      @SuppressWarnings("unchecked")
      // <editor-fold defaultstate="collapsed" desc="Generated Code">
      private void initComponents() {

      jProgressBar1 = new javax.swing.JProgressBar();

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
      this.setLayout(layout);
      layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
      .addGap(50, 50, 50)
      .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 292, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addContainerGap(95, Short.MAX_VALUE))
      );
      layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
      .addContainerGap(197, Short.MAX_VALUE)
      .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addGap(73, 73, 73))
      );
      }// </editor-fold>
      // Variables declaration - do not modify
      private javax.swing.JProgressBar jProgressBar1;
      // End of variables declaration
      }
      -------------------------------------------------------------------------

      /*
      * To change this template, choose Tools | Templates
      * and open the template in the editor.
      */
      package tool.os;

      import java.awt.Color;
      import java.awt.FlowLayout;
      import javax.swing.JProgressBar;
      import javax.swing.UIManager;
      import javax.swing.plaf.ColorUIResource;

      public class ProBarColor extends JProgressBar {

      @Override
      public void setValue(int n) {
      super.setValue(n);
      this.setColor();
      }

      public void setColor() {
      System.out.println("dsfasdf");
      try {
      for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
      if ("Nimbus".equals(laf.getName())) {
      UIManager.setLookAndFeel(laf.getClassName());
      ColorUIResource colorResource = null;
      if (this.getValue() > 90) {
      colorResource = new ColorUIResource(Color.green.darker().darker());
      } else if (getValue() > 80 && getValue() >= 90) {
      colorResource = new ColorUIResource(Color.green.darker());
      } else if (getValue() > 70 && getValue() >= 80) {
      colorResource = new ColorUIResource(Color.green);
      } else if (getValue() > 60 && getValue() >= 70) {
      colorResource = new ColorUIResource(Color.yellow.darker().darker());
      } else if (getValue() > 50 && getValue() >= 60) {
      colorResource = new ColorUIResource(Color.yellow.darker());
      } else if (getValue() > 40 && getValue() >= 50) {
      colorResource = new ColorUIResource(Color.orange.darker());
      } else if (getValue() > 30 && getValue() >= 40) {
      colorResource = new ColorUIResource(Color.orange);
      } else if (getValue() > 20 && getValue() >= 30) {
      colorResource = new ColorUIResource(Color.pink.darker());
      } else if (getValue() > 10 && getValue() >= 20) {
      colorResource = new ColorUIResource(Color.red.darker());
      } else if (getValue() >= 0 && getValue() >= 10) {
      colorResource = new ColorUIResource(Color.red);
      } else if (getValue() > 0) {
      colorResource = new ColorUIResource(Color.BLACK);
      }
      UIManager.getLookAndFeelDefaults().put("nimbusOrange", colorResource);
      }
      }
      } catch (Exception e) {
      e.printStackTrace();
      }
      }
      }


      please help me !
        • 1. Re: Set progressbar's color by value
          jduprez
          Hello,
          you didn't provide an SSCCE so I can't execute your code, nor the suggested workaround, so take it as is with no guarantee :o)

          I am almost positive that the UIDefaults in the UIManager are not re-read automatically after a component's UI has been installed, even if you've changed the colors there.
          You have at least to call udateUI (but I'm not sure this will be enough, as maybe some other level of caching may hamper re-reading the new color you've just put).

          J.