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

JCheckBox does not repaint when setOpaque is toggled.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 6
    • client-libs

      FULL PRODUCT VERSION :
      java version "1.6.0_01"
      Java(TM) SE Runtime Environment (build 1.6.0_01-b06)
      Java HotSpot(TM) Client VM (build 1.6.0_01-b06, mixed mode, sharing)

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

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      Nothing relevant

      A DESCRIPTION OF THE PROBLEM :
      When a JCheckBox is made transparent by means of setOpaque(false) the underlying component does not show through. For example, when you change the background of a JCheckBox to Color.RED, then repeatedly toggle its Opaque state, the background will remain red, no matter what the Opaque state. It is obviously a repaint problem because mousing over the checkbox will repaint the current state correctly.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Run the sample code
      2. Click the JButton on the right
      3. Realize that background color is not changed
      4. move mouse cursor over the checkbox and background color changed


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expected is that the background color is red when opaque=true and original background color is restored when opaque=false.
      ACTUAL -
      Actual result was that toggling the opaque state has no visual effect, the component's background remains red.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.BorderLayout;
      import java.awt.Color;
      import java.awt.Container;
      import java.awt.event.ActionEvent;

      import javax.swing.AbstractAction;
      import javax.swing.Action;
      import javax.swing.JButton;
      import javax.swing.JCheckBox;
      import javax.swing.JFrame;

      public class CheckBoxBug {
        public static void main(String[] args) {
          JFrame frame = new JFrame("CheckBoxBug demo");
          
          Container contentPane = frame.getContentPane();
          
          contentPane.setLayout(new BorderLayout());
          
          final JCheckBox cb = new JCheckBox("My CheckBox");
          
          cb.setOpaque(false);
          
          contentPane.add(cb, BorderLayout.WEST);
          
          Action undoAction = new AbstractAction("<html>Click here to Change JCheckBox<br>from Opaque=true,bg=Color.RED to<br>Opaque=false and vice versa</html>") {
            public void actionPerformed(ActionEvent e) {
              if(cb.isOpaque()) {
                cb.setOpaque(false);
              }
              else {
                cb.setBackground(Color.RED);
                cb.setOpaque(true);
              }
              // cb.repaint(); // Work-around
            }
          };
          
          contentPane.add(new JButton(undoAction), BorderLayout.EAST);
          
          frame.pack();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);
        }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      See the submitted source code, uncomment the line marked with "Work-around".

            Unassigned Unassigned
            ryeung Roger Yeung (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: