JDK-8041561 : Inconsistent opacity behaviour between JCheckBox and JRadioButton
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 7u51
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_7
  • CPU: x86
  • Submitted: 2014-03-20
  • Updated: 2020-12-15
  • Resolved: 2014-05-21
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9 Other
9 b22Fixed openjdk8u292Fixed
Related Reports
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 32-Bit Server VM (build 24.51-b03, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]

A DESCRIPTION OF THE PROBLEM :
When using the MetalLookAndFeel and the DefaultMetalTheme, a JCheckBox can be set to have a transparent background but the JRadioButton can't.

I am using setOpaque(false) to set transparency on both the components. When comparing MetalIconFactory.CheckBoxIcon.paintIcon() with MetalIconFactory.RadioButtonIcon.paintIcon() it is clear that only RadioButtonIcon will always fillRect. CheckBoxIcon will not fillRect unless the checkbox is pressed.



STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
See Source Code

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Both JRadioButon and JCheckBox should be able to have transparent backgrounds by setting setOpaque(false)
ACTUAL -
Only JCheckBox can have a transparent background when the DefaultMetalTheme and MetalLookAndFeel are used.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
Compile the following 2 classes and run Main.main()


public class Main {
    public static MainFrame ex = null;

    public Main() {
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme());
                    UIManager.setLookAndFeel(new MetalLookAndFeel());
                } catch (UnsupportedLookAndFeelException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }

                ex = new MainFrame();
                ex.setVisible(true);
            }
        });
    }

}


public class MainFrame extends JFrame
{
    public MainFrame() {
        initUI();
    }

    private void initUI() {
        JPanel panel = new JPanel();
        panel.setBackground(Color.GREEN);
        getContentPane().add(panel);

        panel.setLayout(new FlowLayout());

        JCheckBox cb = new JCheckBox();
        cb.setOpaque(false);
        panel.add(cb);

        JRadioButton rb = new JRadioButton();
        rb.setOpaque(false);
        panel.add(rb);

        setTitle("Checkbox is tranparent but radio button is not");
        setSize(600, 400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

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


Comments
Ideally, JDK-8063107 should have waited for this change to go in. As is, that additional change should be handed on top of this fix, rather than disrupting a clean backport. I've filed JDK-8258430 for the follow-up change and am approving this as a clean backport.
15-12-2020

8u backport of JDK-8063107 removed changes to test/javax/swing/JRadioButton/8041561/bug8041561.java, since that test is part of this issue, which has not been approved for 8u backport. If it eventually is, then the changes from JDK-8063107 and any future backportst that include bug8041561.java, will have to be part of its backport.
26-11-2020

Patch is identical (except for the first hunk inserted at line 1251 instead of line 1250 => no effective difference) to http://icedtea.classpath.org//hg/icedtea8-forest/jdk?cmd=changeset;node=f79ab27adf43.
04-11-2020

Fix Request (OpenJDK 8u): The patch applies cleanly on current jdk8u-dev. It introduces a new test that fails prior to applying the other changes, and passes afterwards. Comment added on behalf of Jonathan Dowland.
03-11-2020

The problem is reproducible on jdk8u05.
23-04-2014