Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6289684

Way to change the color of disabled JCheckbox text

    Details

    • Type: Enhancement
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 5.0
    • Fix Version/s: None
    • Component/s: client-libs

      Description

      A DESCRIPTION OF THE REQUEST :
      There is no way to set the color of the text of the JCheckBox if it is disabled. It is necessary to assign a color for the label for the case the component is disabled. Lnf is Windows.

      JUSTIFICATION :
      Not in all use cases it is enough to have the default weak gray for the disabled component but e.g. a normal strong black (as it would be if the component is enabled).

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      While calling a method of the checkbox the color of the text of the disabled JCheckBox changes to the value you assigned. A new solution via UIManager could also be possible but it would assign the new color to alle checkboxes and there are checkboxes which should be colored as default. A fine-grained solution on component base is necessary.
      ACTUAL -
      There is no way to assign a new color to the text of the disabled checkbox.

      ---------- BEGIN SOURCE ----------
      import javax.swing.JCheckBox;
      import javax.swing.JFrame;
      import javax.swing.UIManager;
      import javax.swing.UnsupportedLookAndFeelException;

      public class CheckBoxTestCase {

      public static void main (String[]args)
      {
      try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      }
      catch (InstantiationException inex)
      {
      inex.printStackTrace();
      }
      catch (IllegalAccessException acex)
      {
      acex.printStackTrace();
      }
      catch (ClassNotFoundException cnfex)
      {
      cnfex.printStackTrace();
      }
      catch (UnsupportedLookAndFeelException lnfex)
      {
      lnfex.printStackTrace();
      }

      JFrame frame = new JFrame();
      JCheckBox checkbox = new JCheckBox("checkme");

      checkbox.setEnabled(false);

      frame.add(checkbox);
      frame.pack();
      frame.setVisible(true);
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Step 1:
      UIManager.getDefaults().put("CheckBox.darkShadow", Color.BLACK);

      Step 2:
      I assign a new CheckBoxUI to the checkbox:
         setUI(new CheckBoxUI(this));

      public class CheckBoxUI extends BasicCheckBoxUI
      {
          protected void paintText(Graphics g, AbstractButton b, Rectangle textRect, String text)
          {
           ButtonModel model = b.getModel();

              FontMetrics fm = g.getFontMetrics();

              //I need this here, only for showing a possible workaround.
              BasicGraphicsUtils.drawString(g,text, model.getMnemonic(),
                      textRect.x,
                      textRect.y + fm.getAscent());
              
              g.setColor(Color.black);

              if(b.hasFocus())
              {
                   b.setBorder(dtb);
              }
              else
              {
                   //a default border
                   b.setBorder(CheckBox.defaultBorder);
              }
          }
          
          public CheckBoxUI(CheckBox cb)
          {
           this.cb=cb;
          }
          
          private CheckBox cb;
      }

      Step 3:
      Paint an own focus border. If the new workaround checkbox has the focus it does not show the dot border to mark the component as focussed. Thus it has to be painted manually. For this AbstractBorder has to be extended and used.
      ###@###.### 2005-06-23 00:27:40 GMT

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              gmanwanisunw Girish Manwani (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Imported:
                Indexed: