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

Check Box does not paint if XP theme is Windows Classic

    Details

      Description

      FULL PRODUCT VERSION :
      java version "1.5.0_05"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_05-b05)
      Java HotSpot(TM) Client VM (build 1.5.0_05-b05, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windos XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      On XP, if the user has the Windows Classic theme, then the checkboxes do not paint. The text for the checkbox, and the background of the checkbox paint, just not the white square with or without the tick. The checkbox still behaves correctly.

      The output from DegugGraphics shows that something is getting a null Color from the theme.

      Graphics(15-3) Setting color: javax.swing.plaf.ColorUIResource[r=0,g=0,b=0]
      Graphics(15-3) Setting font: javax.swing.plaf.FontUIResource[family=Tahoma,name=Tahoma,style=plain,size=11]
      Graphics(17-3) Setting color: javax.swing.plaf.ColorUIResource[r=212,g=208,b=200]
      Graphics(17-3) Filling rect: java.awt.Rectangle[x=0,y=0,width=131,height=23]
      Graphics(17-3) Setting font: javax.swing.plaf.FontUIResource[family=Tahoma,name=Tahoma,style=plain,size=11]
      Graphics(17-3) Setting color: javax.swing.plaf.ColorUIResource[r=212,g=208,b=200]
      Graphics(17-3) Filling rect: java.awt.Rectangle[x=0,y=0,width=131,height=23]
      Graphics(17-3) Setting color: null
      Graphics(17-3) Drawing line: from (4, 5) to (15, 5)
      Graphics(17-3) Drawing line: from (4, 6) to (4, 16)
      Graphics(17-3) Setting color: null
      Graphics(17-3) Drawing line: from (16, 5) to (16, 17)
      Graphics(17-3) Drawing line: from (4, 17) to (15, 17)
      Graphics(17-3) Setting color: null
      Graphics(17-3) Drawing line: from (5, 6) to (14, 6)
      Graphics(17-3) Drawing line: from (5, 7) to (5, 15)
      Graphics(17-3) Setting color: null
      Graphics(17-3) Drawing line: from (5, 16) to (15, 16)
      Graphics(17-3) Drawing line: from (15, 6) to (15, 15)
      Graphics(17-3) Setting color: null
      Graphics(17-3) Filling rect: java.awt.Rectangle[x=6,y=7,width=9,height=9]
      Graphics(17-3) Setting color: null
      Graphics(17-3) Setting color: javax.swing.plaf.ColorUIResource[r=0,g=0,b=0]
      Graphics(17-3) Drawing string: "My checkbox text" at: java.awt.Point[x=21,y=15]

      The above output is for the paint of the checkbox being unselected.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Using XP:
      Change theme to classic
      Start a swing app that has a JCheckBox

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      That you can see the white square of the checkbox, and the black tick if the checkbox is selected
      ACTUAL -
      The white square anb black tick never paint

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.Component;
      import javax.swing.LookAndFeel;
      import javax.swing.SwingUtilities;
      import javax.swing.UIManager;

      public class CompanyLogin {
          private static final CompanyLoginFrame2 frame = new CompanyLoginFrame2();

          private static void run() {
              useSystemLookAndFeel(frame);
              frame.setVisible(true);
          }

          /**
           * @param args
           */
          public static void main(String[] args) {
              CompanyLogin.run();
          }

          private static void useSystemLookAndFeel(Component component) {
              try {
                  LookAndFeel oldLookAndFeel = UIManager.getLookAndFeel();
                  String lookAndFeelClassName =
                          UIManager.getSystemLookAndFeelClassName();
                  UIManager.setLookAndFeel(lookAndFeelClassName);
                  fixWindowsLookAndFeel(lookAndFeelClassName);
                  
                  //Problem occurs here after calling updateComponentTreeUI!!!
                  SwingUtilities.updateComponentTreeUI(component);
                  
                  
                  UIManager.setLookAndFeel(oldLookAndFeel);
              } catch (Exception e) {
                  // who cares?
              }
          }

          private static void fixWindowsLookAndFeel(String lookAndFeelClassName) {
              if ("com.sun.java.swing.plaf.windows.WindowsLookAndFeel".equals(
                      lookAndFeelClassName)) {
                  UIManager.put("FormattedTextFieldUI",
                          "com.sun.java.swing.plaf.windows.WindowsTextFieldUI");
                  UIManager.put("PasswordField.font",
                          UIManager.get("TextField.font"));
              }
          }
          
          private static class CompanyLoginFrame2 extends javax.swing.JFrame {
              
              /** Creates new form CompanyLoginFrame */
              public CompanyLoginFrame2() {
                  initComponents();
              }
              
              /** 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.
               */
              // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
              //GEN-BEGIN:initComponents
              private void initComponents() {
                  java.awt.GridBagConstraints gridBagConstraints;

                  viaInternetCheckBox = new JCheckBoxThatWorksOnXPWithClassicThemeSelected();

                  getContentPane().setLayout(new java.awt.GridBagLayout());

                  setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
                  setResizable(false);

                  viaInternetCheckBox.setText("Via Internet");
                  viaInternetCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
                  viaInternetCheckBox.setMargin(new java.awt.Insets(0, 0, 0,0));
                  gridBagConstraints = new java.awt.GridBagConstraints();
                  gridBagConstraints.gridx = 0;
                  gridBagConstraints.gridy = 3;
                  gridBagConstraints.gridwidth = 2;
                  gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
                  gridBagConstraints.insets = new java.awt.Insets(10, 40, 40, 0);
                  getContentPane().add(viaInternetCheckBox, gridBagConstraints);
                  pack();
              }// </editor-fold>//GEN-END:initComponents
              
              // Variables declaration - do not modify//GEN-BEGIN:variables
              protected JCheckBoxThatWorksOnXPWithClassicThemeSelected viaInternetCheckBox;
              // End of variables declaration//GEN-END:variables
              
          }

      }
      ---------- END SOURCE ----------


      CUSTOMER SUBMITTED WORKAROUND :
      Don't use JCheckbox, use a subclass that manually paints the white box and tick:


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

      //NOTE: This code is will force checkboxes to behave like this regardless of
      //theme. E.G. the XP Standard theme seems to draw icons instead of manually
      //painting lines
      public class JCheckBoxThatWorksOnXPWithClassicThemeSelected extends JCheckBox {


          @Override
          public void paint(Graphics g) {
              super.paint(g);

              g.setColor(Color.WHITE);
              g.drawLine(4,5,15,5);
              g.drawLine(4,6,4,16);
              g.drawLine(16,5,16,17);
              g.drawLine(4,17,15,17);
              g.drawLine(5,6,14,6);
              g.drawLine(5,7,5,15);
              g.drawLine(5,16,15,16);
              g.drawLine(15,6,15,15);
              g.fillRect(6,7,9,9);
              
            if(this.isSelected()){
                g.setColor(Color.BLACK);
                g.drawLine(13,8,13,8);
                g.drawLine(12,9,13,9);
                g.drawLine(11,10,13,10);
                g.drawLine(10,11,12,11);
                g.drawLine(7,12,11,12);
                g.drawLine(8,13,10,13);
                g.drawLine(9,14,9,14);
                g.drawLine(7,10,7,10);
                g.drawLine(7,11,8,11);

            }
          }
      }

        Attachments

          Activity

            People

            • Assignee:
              bchristi Brent Christian
              Reporter:
              ryeung Roger Yeung (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: