Hi, this is my 2nd attempt to fix the RadioButton issue. I found out that BasicRadioButtonUI is supposed to override getPrefferedSize. This makes it possible to implement the layout stuff which properly notices the default icon which is used when the icon property is not set.
ChangeLog: 2006-06-16 Robert Schuster <[EMAIL PROTECTED]> * javax/swing/plaf/basic/BasicRadioButtonUI.java: (installDefaults): Removed unneccessary code. (paint): Removed complex if-cascade, revert to default icon if icon property is not set. (getPreferredSize): New method. cya Robert
Index: javax/swing/plaf/basic/BasicRadioButtonUI.java =================================================================== RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicRadioButtonUI.java,v retrieving revision 1.18 diff -u -r1.18 BasicRadioButtonUI.java --- javax/swing/plaf/basic/BasicRadioButtonUI.java 13 Jun 2006 09:28:57 -0000 1.18 +++ javax/swing/plaf/basic/BasicRadioButtonUI.java 16 Jun 2006 12:26:50 -0000 @@ -42,6 +42,7 @@ import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; +import java.awt.Insets; import java.awt.Rectangle; import javax.swing.AbstractButton; @@ -92,14 +93,6 @@ protected void installDefaults(AbstractButton b) { super.installDefaults(b); - if (b.getIcon() == null) - b.setIcon(icon); - if (b.getSelectedIcon() == null) - b.setSelectedIcon(icon); - if (b.getDisabledIcon() == null) - b.setDisabledIcon(icon); - if (b.getDisabledSelectedIcon() == null) - b.setDisabledSelectedIcon(icon); } /** @@ -145,16 +138,17 @@ g.setFont(f); ButtonModel m = b.getModel(); - Icon currentIcon = null; - if (m.isSelected() && m.isEnabled()) - currentIcon = b.getSelectedIcon(); - else if (! m.isSelected() && m.isEnabled()) - currentIcon = b.getIcon(); - else if (m.isSelected() && ! m.isEnabled()) - currentIcon = b.getDisabledSelectedIcon(); - else // (!m.isSelected() && ! m.isEnabled()) - currentIcon = b.getDisabledIcon(); + // FIXME: Do a filtering on any customized icon if the following property + // is set. + boolean enabled = b.isEnabled(); + + Icon currentIcon = b.getIcon(); + if (currentIcon == null) + { + currentIcon = getDefaultIcon(); + } + SwingUtilities.calculateInnerArea(b, vr); String text = SwingUtilities.layoutCompoundLabel(c, g.getFontMetrics(f), b.getText(), currentIcon, @@ -162,15 +156,57 @@ b.getVerticalTextPosition(), b.getHorizontalTextPosition(), vr, ir, tr, b.getIconTextGap() + defaultTextShiftOffset); - if (currentIcon != null) - { - currentIcon.paintIcon(c, g, ir.x, ir.y); - } + currentIcon.paintIcon(c, g, ir.x, ir.y); + if (text != null) paintText(g, b, tr, text); if (b.hasFocus() && b.isFocusPainted() && m.isEnabled()) paintFocus(g, tr, c.getSize()); } + + public Dimension getPreferredSize(JComponent c) + { + // This is basically the same code as in + // BasicGraphicsUtils.getPreferredButtonSize() but takes the default icon + // property into account. JRadioButton and subclasses always have an icon: + // the check box. If the user explicitly changes it with setIcon() that + // one will be used for layout calculations and painting instead. + // The other icon properties are ignored. + AbstractButton b = (AbstractButton) c; + + Rectangle contentRect; + Rectangle viewRect; + Rectangle iconRect = new Rectangle(); + Rectangle textRect = new Rectangle(); + Insets insets = b.getInsets(); + + Icon i = b.getIcon(); + if (i == null) + i = getDefaultIcon(); + + viewRect = new Rectangle(); + + SwingUtilities.layoutCompoundLabel( + b, // for the component orientation + b.getFontMetrics(b.getFont()), + b.getText(), + i, + b.getVerticalAlignment(), + b.getHorizontalAlignment(), + b.getVerticalTextPosition(), + b.getHorizontalTextPosition(), + viewRect, iconRect, textRect, + defaultTextIconGap + defaultTextShiftOffset); + + contentRect = textRect.union(iconRect); + + return new Dimension(insets.left + + contentRect.width + + insets.right + b.getHorizontalAlignment(), + insets.top + + contentRect.height + + insets.bottom); + } /** * Paints the focus indicator for JRadioButtons.
signature.asc
Description: OpenPGP digital signature