-
Bug
-
Resolution: Duplicate
-
P3
-
None
-
1.4.1
-
x86
-
windows_2000
Name: rmT116609 Date: 08/28/2002
FULL PRODUCT VERSION :
java version "1.4.1-rc"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-rc-b19)
Java HotSpot(TM) Client VM (build 1.4.1-rc-b19, mixed mode)
FULL OPERATING SYSTEM VERSION :
Microsoft Windows 2000 [Version 5.00.2195]
Service Pack 2
ADDITIONAL OPERATING SYSTEMS :
Windows NT Version 4.0
Service Pack 6
A DESCRIPTION OF THE PROBLEM :
WindowsRadioButtonUI.getPreferredSize() throws NullPointerException (under the Windows Look&Feel of course) if a JRadioButton has child Components.
Adding child Components to a JRadioButton may be a rare thing to do but is NOT explicitly disallowed, as far as I know.
This problem doesn't exist in JDK 1.3.x.
Comparing the source code of WindowsRadioButtonUI from 1.3 and 1.4 reveals that getPreferredSize(), shown below, is new in 1.4.
public Dimension getPreferredSize(JComponent c) {
Dimension d = super.getPreferredSize(c);
/* Ensure that the width and height of the button is odd,
* to allow for the focus line if focus is painted
*/
AbstractButton b = (AbstractButton)c;
if (b.isFocusPainted()) {
if(d.width % 2 == 0) { d.width += 1; }
if(d.height % 2 == 0) { d.height += 1; }
}
return d;
}
It first calls super.getPreferredSize() which is in BasicRadioButtonUI. The first 4 lines
public Dimension getPreferredSize(JComponent c) {
if(c.getComponentCount() > 0) {
return null;
}
clearly shows that it returns null if the JComponent (JRadioButton in this case) has any child Components.
WindowsRadioButtonUI.getPreferredSize() doesn't check this condition before using the returned Dimension, thus throwing a NullPointerException.
REGRESSION. Last worked in version 1.3.1
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1.Create a JRadioButton.
2.Add a child component, such as a JLabel, to the JRadioButton.
3.Try to show the JRadioButton on screen.
EXPECTED VERSUS ACTUAL BEHAVIOR :
Expected Result:
No Exceptions should occur and the JRadioButton displays correctly, as it does under JDK 1.3.
Acutal Result:
NullPointerException
ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.NullPointerException
at com.sun.java.swing.plaf.windows.WindowsRadioButtonUI.getPreferredSize(WindowsRadioButtonUI.java:94)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1272)
at java.awt.GridLayout.preferredLayoutSize(GridLayout.java:313)
at java.awt.Container.preferredSize(Container.java:1175)
at java.awt.Container.getPreferredSize(Container.java:1159)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1274)
at com.nastel.nfc.ui.SimpleLayout.preferredLayoutSize(SimpleLayout.java:86)
at java.awt.Container.preferredSize(Container.java:1175)
at java.awt.Container.getPreferredSize(Container.java:1159)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1274)
at javax.swing.plaf.basic.BasicTabbedPaneUI$TabbedPaneLayout.calculateSize(BasicTabbedPaneUI.java:1992)
at javax.swing.plaf.basic.BasicTabbedPaneUI$TabbedPaneLayout.preferredLayoutSize(BasicTabbedPaneUI.java:1966)
at java.awt.Container.preferredSize(Container.java:1175)
at java.awt.Container.getPreferredSize(Container.java:1159)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1274)
at java.awt.BorderLayout.preferredLayoutSize(BorderLayout.java:564)
at java.awt.Container.preferredSize(Container.java:1175)
at java.awt.Container.getPreferredSize(Container.java:1159)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1274)
at java.awt.BorderLayout.preferredLayoutSize(BorderLayout.java:564)
at java.awt.Container.preferredSize(Container.java:1175)
at java.awt.Container.getPreferredSize(Container.java:1159)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1274)
at javax.swing.JRootPane$RootLayout.preferredLayoutSize(JRootPane.java:816)
at java.awt.Container.preferredSize(Container.java:1175)
at java.awt.Container.getPreferredSize(Container.java:1159)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1274)
at java.awt.BorderLayout.preferredLayoutSize(BorderLayout.java:564)
at java.awt.Container.preferredSize(Container.java:1175)
at java.awt.Container.getPreferredSize(Container.java:1159)
at java.awt.Window.pack(Window.java:430)
at com.nastel.nfc.ui.admin.statustree.SensorPropertiesPanel.main(SensorPropertiesPanel.java:594)
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
/* Run this program under both 1.3 and 1.4 and compare results. */
import java.awt.*;
import javax.swing.*;
public class RadioButtonUITest
{
public static void main(String[] arg)
{
try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }
catch (Exception e) { }
JLabel floppy = new JLabel("Floppy Drive", UIManager.getIcon("FileView.floppyDriveIcon"), JLabel.LEFT);
floppy.setBorder(BorderFactory.createEmptyBorder(0, 16, 0, 0));
JRadioButton floppyRadio = new JRadioButton();
floppyRadio.add(floppy);
JLabel hard = new JLabel("Hard Drive", UIManager.getIcon("FileView.hardDriveIcon"), JLabel.LEFT);
hard.setBorder(BorderFactory.createEmptyBorder(0, 16, 0, 0));
JRadioButton hardRadio = new JRadioButton();
hardRadio.add(hard);
JFrame frame = new JFrame("RadioButtonUI Test");
frame.setBounds(200, 200, 200, 100);
frame.getContentPane().add(floppyRadio, BorderLayout.NORTH);
frame.getContentPane().add(hardRadio, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);;
frame.show();
}
}
---------- END SOURCE ----------
CUSTOMER WORKAROUND :
None.
Release Regression From : 1.3.1_04
The above release value was the last known release where this
bug was known to work. Since then there has been a regression.
(Review ID: 163746)
======================================================================
- duplicates
-
JDK-4694008 1.4.0 REGRESSION: NPE in preferred size for button with two labels in Windows LA
-
- Resolved
-