Skip to content

Commit 6c48f4e

Browse files
committed
8348760: RadioButton is not shown if JRadioButtonMenuItem is rendered with ImageIcon in WindowsLookAndFeel
Reviewed-by: prr, aivanov Backport-of: e29346d
1 parent 73c28c2 commit 6c48f4e

File tree

8 files changed

+549
-106
lines changed

8 files changed

+549
-106
lines changed

src/java.desktop/share/classes/com/sun/java/swing/SwingUtilities3.java

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2002, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,22 +26,30 @@
2626
package com.sun.java.swing;
2727

2828
import java.applet.Applet;
29+
import java.awt.Color;
2930
import java.awt.Component;
3031
import java.awt.Container;
3132
import java.awt.Graphics;
3233
import java.awt.Graphics2D;
34+
import java.awt.Insets;
35+
import java.awt.Rectangle;
3336
import java.awt.Stroke;
3437
import java.awt.Window;
3538
import java.awt.geom.AffineTransform;
3639
import java.util.Collections;
3740
import java.util.Map;
3841
import java.util.WeakHashMap;
3942

43+
import javax.swing.ButtonModel;
44+
import javax.swing.Icon;
4045
import javax.swing.JComponent;
46+
import javax.swing.JMenu;
4147
import javax.swing.RepaintManager;
4248

4349
import sun.awt.AppContext;
4450
import sun.awt.SunToolkit;
51+
import sun.swing.MenuItemLayoutHelper;
52+
import sun.swing.SwingUtilities2;
4553

4654
import static sun.java2d.pipe.Region.clipRound;
4755

@@ -64,6 +72,10 @@ public class SwingUtilities3 {
6472
private static final Object DELEGATE_REPAINT_MANAGER_KEY =
6573
new StringBuilder("DelegateRepaintManagerKey");
6674

75+
private static Color disabledForeground;
76+
private static Color acceleratorSelectionForeground;
77+
private static Color acceleratorForeground;
78+
6779
/**
6880
* Registers delegate RepaintManager for {@code JComponent}.
6981
*/
@@ -143,6 +155,128 @@ public static RepaintManager getDelegateRepaintManager(Component
143155
return delegate;
144156
}
145157

158+
public static void applyInsets(Rectangle rect, Insets insets) {
159+
if (insets != null) {
160+
rect.x += insets.left;
161+
rect.y += insets.top;
162+
rect.width -= (insets.right + rect.x);
163+
rect.height -= (insets.bottom + rect.y);
164+
}
165+
}
166+
167+
public static void paintCheckIcon(Graphics g, MenuItemLayoutHelper lh,
168+
MenuItemLayoutHelper.LayoutResult lr,
169+
Color holdc, Color foreground) {
170+
if (lh.getCheckIcon() != null) {
171+
ButtonModel model = lh.getMenuItem().getModel();
172+
if (model.isArmed() || (lh.getMenuItem() instanceof JMenu
173+
&& model.isSelected())) {
174+
g.setColor(foreground);
175+
} else {
176+
g.setColor(holdc);
177+
}
178+
if (lh.useCheckAndArrow()) {
179+
lh.getCheckIcon().paintIcon(lh.getMenuItem(), g,
180+
lr.getCheckRect().x, lr.getCheckRect().y);
181+
}
182+
g.setColor(holdc);
183+
}
184+
}
185+
186+
public static void paintIcon(Graphics g, MenuItemLayoutHelper lh,
187+
MenuItemLayoutHelper.LayoutResult lr, Color holdc) {
188+
if (lh.getIcon() != null) {
189+
Icon icon;
190+
ButtonModel model = lh.getMenuItem().getModel();
191+
if (!model.isEnabled()) {
192+
icon = lh.getMenuItem().getDisabledIcon();
193+
} else if (model.isPressed() && model.isArmed()) {
194+
icon = lh.getMenuItem().getPressedIcon();
195+
if (icon == null) {
196+
// Use default icon
197+
icon = lh.getMenuItem().getIcon();
198+
}
199+
} else {
200+
icon = lh.getMenuItem().getIcon();
201+
}
202+
203+
if (icon != null) {
204+
icon.paintIcon(lh.getMenuItem(), g, lr.getIconRect().x,
205+
lr.getIconRect().y);
206+
g.setColor(holdc);
207+
}
208+
}
209+
}
210+
211+
212+
public static void paintAccText(Graphics g, MenuItemLayoutHelper lh,
213+
MenuItemLayoutHelper.LayoutResult lr) {
214+
if (!lh.getAccText().isEmpty()) {
215+
ButtonModel model = lh.getMenuItem().getModel();
216+
g.setFont(lh.getAccFontMetrics().getFont());
217+
if (!model.isEnabled()) {
218+
219+
// paint the accText disabled
220+
if (disabledForeground != null) {
221+
g.setColor(disabledForeground);
222+
SwingUtilities2.drawString(lh.getMenuItem(), g,
223+
lh.getAccText(), lr.getAccRect().x,
224+
lr.getAccRect().y + lh.getAccFontMetrics().getAscent());
225+
} else {
226+
g.setColor(lh.getMenuItem().getBackground().brighter());
227+
SwingUtilities2.drawString(lh.getMenuItem(), g,
228+
lh.getAccText(), lr.getAccRect().x,
229+
lr.getAccRect().y + lh.getAccFontMetrics().getAscent());
230+
g.setColor(lh.getMenuItem().getBackground().darker());
231+
SwingUtilities2.drawString(lh.getMenuItem(), g,
232+
lh.getAccText(), lr.getAccRect().x - 1,
233+
lr.getAccRect().y + lh.getFontMetrics().getAscent() - 1);
234+
}
235+
} else {
236+
237+
// paint the accText normally
238+
if (model.isArmed()
239+
|| (lh.getMenuItem() instanceof JMenu
240+
&& model.isSelected())) {
241+
g.setColor(acceleratorSelectionForeground);
242+
} else {
243+
g.setColor(acceleratorForeground);
244+
}
245+
SwingUtilities2.drawString(lh.getMenuItem(), g, lh.getAccText(),
246+
lr.getAccRect().x, lr.getAccRect().y +
247+
lh.getAccFontMetrics().getAscent());
248+
}
249+
}
250+
}
251+
252+
public static void setDisabledForeground(Color disabledFg) {
253+
disabledForeground = disabledFg;
254+
}
255+
256+
public static void setAcceleratorSelectionForeground(Color acceleratorSelectionFg) {
257+
acceleratorSelectionForeground = acceleratorSelectionFg;
258+
}
259+
260+
public static void setAcceleratorForeground(Color acceleratorFg) {
261+
acceleratorForeground = acceleratorFg;
262+
}
263+
264+
public static void paintArrowIcon(Graphics g, MenuItemLayoutHelper lh,
265+
MenuItemLayoutHelper.LayoutResult lr,
266+
Color foreground) {
267+
if (lh.getArrowIcon() != null) {
268+
ButtonModel model = lh.getMenuItem().getModel();
269+
if (model.isArmed() || (lh.getMenuItem() instanceof JMenu
270+
&& model.isSelected())) {
271+
g.setColor(foreground);
272+
}
273+
if (lh.useCheckAndArrow()) {
274+
lh.getArrowIcon().paintIcon(lh.getMenuItem(), g,
275+
lr.getArrowRect().x, lr.getArrowRect().y);
276+
}
277+
}
278+
}
279+
146280
/**
147281
* A task which paints an <i>unscaled</i> border after {@code Graphics}
148282
* transforms are removed. It's used with the

src/java.desktop/share/classes/javax/swing/plaf/basic/BasicMenuItemUI.java

Lines changed: 51 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,17 +25,52 @@
2525

2626
package javax.swing.plaf.basic;
2727

28-
import java.awt.*;
29-
import java.awt.event.*;
28+
import java.awt.Color;
29+
import java.awt.Component;
30+
import java.awt.Dimension;
31+
import java.awt.Font;
32+
import java.awt.FontMetrics;
33+
import java.awt.Graphics;
34+
import java.awt.Insets;
35+
import java.awt.Point;
36+
import java.awt.Rectangle;
37+
import java.awt.event.ActionEvent;
38+
import java.awt.event.InputEvent;
39+
import java.awt.event.MouseEvent;
3040
import java.beans.PropertyChangeEvent;
3141
import java.beans.PropertyChangeListener;
3242

33-
import javax.swing.*;
34-
import javax.swing.event.*;
35-
import javax.swing.plaf.*;
43+
import javax.swing.ButtonModel;
44+
import javax.swing.Icon;
45+
import javax.swing.InputMap;
46+
import javax.swing.JCheckBoxMenuItem;
47+
import javax.swing.JComponent;
48+
import javax.swing.JMenu;
49+
import javax.swing.JMenuItem;
50+
import javax.swing.JRadioButtonMenuItem;
51+
import javax.swing.KeyStroke;
52+
import javax.swing.LookAndFeel;
53+
import javax.swing.MenuElement;
54+
import javax.swing.MenuSelectionManager;
55+
import javax.swing.SwingUtilities;
56+
import javax.swing.UIManager;
57+
import javax.swing.event.MenuDragMouseEvent;
58+
import javax.swing.event.MenuDragMouseListener;
59+
import javax.swing.event.MenuKeyListener;
60+
61+
import javax.swing.event.MouseInputListener;
62+
import javax.swing.plaf.ComponentInputMapUIResource;
63+
import javax.swing.plaf.ComponentUI;
64+
import javax.swing.plaf.MenuItemUI;
65+
import javax.swing.plaf.UIResource;
3666
import javax.swing.text.View;
3767

38-
import sun.swing.*;
68+
import com.sun.java.swing.SwingUtilities3;
69+
import sun.swing.MenuItemCheckIconFactory;
70+
import sun.swing.MenuItemLayoutHelper;
71+
import sun.swing.SwingUtilities2;
72+
import sun.swing.UIAction;
73+
3974

4075
/**
4176
* BasicMenuItem implementation
@@ -670,84 +705,22 @@ protected void paintMenuItem(Graphics g, JComponent c,
670705

671706
private void paintIcon(Graphics g, MenuItemLayoutHelper lh,
672707
MenuItemLayoutHelper.LayoutResult lr, Color holdc) {
673-
if (lh.getIcon() != null) {
674-
Icon icon;
675-
ButtonModel model = lh.getMenuItem().getModel();
676-
if (!model.isEnabled()) {
677-
icon = lh.getMenuItem().getDisabledIcon();
678-
} else if (model.isPressed() && model.isArmed()) {
679-
icon = lh.getMenuItem().getPressedIcon();
680-
if (icon == null) {
681-
// Use default icon
682-
icon = lh.getMenuItem().getIcon();
683-
}
684-
} else {
685-
icon = lh.getMenuItem().getIcon();
686-
}
687-
688-
if (icon != null) {
689-
icon.paintIcon(lh.getMenuItem(), g, lr.getIconRect().x,
690-
lr.getIconRect().y);
691-
g.setColor(holdc);
692-
}
693-
}
708+
SwingUtilities3.paintIcon(g, lh, lr, holdc);
694709
}
695710

696711
private void paintCheckIcon(Graphics g, MenuItemLayoutHelper lh,
697712
MenuItemLayoutHelper.LayoutResult lr,
698713
Color holdc, Color foreground) {
699-
if (lh.getCheckIcon() != null) {
700-
ButtonModel model = lh.getMenuItem().getModel();
701-
if (model.isArmed() || (lh.getMenuItem() instanceof JMenu
702-
&& model.isSelected())) {
703-
g.setColor(foreground);
704-
} else {
705-
g.setColor(holdc);
706-
}
707-
if (lh.useCheckAndArrow()) {
708-
lh.getCheckIcon().paintIcon(lh.getMenuItem(), g,
709-
lr.getCheckRect().x, lr.getCheckRect().y);
710-
}
711-
g.setColor(holdc);
712-
}
714+
SwingUtilities3.paintCheckIcon(g, lh, lr, holdc, foreground);
713715
}
714716

715717
private void paintAccText(Graphics g, MenuItemLayoutHelper lh,
716718
MenuItemLayoutHelper.LayoutResult lr) {
717-
if (!lh.getAccText().isEmpty()) {
718-
ButtonModel model = lh.getMenuItem().getModel();
719-
g.setFont(lh.getAccFontMetrics().getFont());
720-
if (!model.isEnabled()) {
721-
// *** paint the accText disabled
722-
if (disabledForeground != null) {
723-
g.setColor(disabledForeground);
724-
SwingUtilities2.drawString(lh.getMenuItem(), g,
725-
lh.getAccText(), lr.getAccRect().x,
726-
lr.getAccRect().y + lh.getAccFontMetrics().getAscent());
727-
} else {
728-
g.setColor(lh.getMenuItem().getBackground().brighter());
729-
SwingUtilities2.drawString(lh.getMenuItem(), g,
730-
lh.getAccText(), lr.getAccRect().x,
731-
lr.getAccRect().y + lh.getAccFontMetrics().getAscent());
732-
g.setColor(lh.getMenuItem().getBackground().darker());
733-
SwingUtilities2.drawString(lh.getMenuItem(), g,
734-
lh.getAccText(), lr.getAccRect().x - 1,
735-
lr.getAccRect().y + lh.getFontMetrics().getAscent() - 1);
736-
}
737-
} else {
738-
// *** paint the accText normally
739-
if (model.isArmed()
740-
|| (lh.getMenuItem() instanceof JMenu
741-
&& model.isSelected())) {
742-
g.setColor(acceleratorSelectionForeground);
743-
} else {
744-
g.setColor(acceleratorForeground);
745-
}
746-
SwingUtilities2.drawString(lh.getMenuItem(), g, lh.getAccText(),
747-
lr.getAccRect().x, lr.getAccRect().y +
748-
lh.getAccFontMetrics().getAscent());
749-
}
750-
}
719+
SwingUtilities3.setDisabledForeground(disabledForeground);
720+
SwingUtilities3.setAcceleratorSelectionForeground(
721+
acceleratorSelectionForeground);
722+
SwingUtilities3.setAcceleratorForeground(acceleratorForeground);
723+
SwingUtilities3.paintAccText(g, lh, lr);
751724
}
752725

753726
private void paintText(Graphics g, MenuItemLayoutHelper lh,
@@ -766,26 +739,11 @@ private void paintText(Graphics g, MenuItemLayoutHelper lh,
766739
private void paintArrowIcon(Graphics g, MenuItemLayoutHelper lh,
767740
MenuItemLayoutHelper.LayoutResult lr,
768741
Color foreground) {
769-
if (lh.getArrowIcon() != null) {
770-
ButtonModel model = lh.getMenuItem().getModel();
771-
if (model.isArmed() || (lh.getMenuItem() instanceof JMenu
772-
&& model.isSelected())) {
773-
g.setColor(foreground);
774-
}
775-
if (lh.useCheckAndArrow()) {
776-
lh.getArrowIcon().paintIcon(lh.getMenuItem(), g,
777-
lr.getArrowRect().x, lr.getArrowRect().y);
778-
}
779-
}
742+
SwingUtilities3.paintArrowIcon(g, lh, lr, foreground);
780743
}
781744

782745
private void applyInsets(Rectangle rect, Insets insets) {
783-
if(insets != null) {
784-
rect.x += insets.left;
785-
rect.y += insets.top;
786-
rect.width -= (insets.right + rect.x);
787-
rect.height -= (insets.bottom + rect.y);
788-
}
746+
SwingUtilities3.applyInsets(rect, insets);
789747
}
790748

791749
/**

0 commit comments

Comments
 (0)