Skip to content

Commit a72e14a

Browse files
tkiriyamaGoeLin
authored andcommitted
7093691: Nimbus LAF: disabled JComboBox using renderer has bad font color
Backport-of: 87b314a985c5c3937c1d1d8daadd3e9f8b1acd9d
1 parent 70a227f commit a72e14a

File tree

3 files changed

+232
-9
lines changed

3 files changed

+232
-9
lines changed

src/java.desktop/share/classes/javax/swing/DefaultListCellRenderer.java

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2023, 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,15 +25,15 @@
2525

2626
package javax.swing;
2727

28-
import javax.swing.*;
29-
import javax.swing.event.*;
30-
import javax.swing.border.*;
31-
32-
import java.awt.Component;
3328
import java.awt.Color;
29+
import java.awt.Component;
3430
import java.awt.Rectangle;
35-
3631
import java.io.Serializable;
32+
33+
import javax.swing.border.Border;
34+
import javax.swing.border.EmptyBorder;
35+
import javax.swing.plaf.synth.SynthListUI;
36+
3737
import sun.swing.DefaultLookup;
3838
import sun.swing.SwingUtilities2;
3939

@@ -157,7 +157,11 @@ public Component getListCellRendererComponent(
157157
setText((value == null) ? "" : value.toString());
158158
}
159159

160-
setEnabled(list.isEnabled());
160+
if (list.getName() == null || !list.getName().equals("ComboBox.list")
161+
|| !(list.getUI() instanceof SynthListUI)) {
162+
setEnabled(list.isEnabled());
163+
}
164+
161165
setFont(list.getFont());
162166

163167
Border border = null;

src/java.desktop/share/classes/javax/swing/plaf/synth/SynthComboBoxUI.java

+38-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2002, 2023, 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
@@ -39,6 +39,7 @@
3939

4040
import javax.swing.ComboBoxEditor;
4141
import javax.swing.DefaultButtonModel;
42+
import javax.swing.DefaultListCellRenderer;
4243
import javax.swing.Icon;
4344
import javax.swing.JButton;
4445
import javax.swing.JComboBox;
@@ -108,6 +109,8 @@ public class SynthComboBoxUI extends BasicComboBoxUI implements
108109
*/
109110
private EditorFocusHandler editorFocusHandler;
110111

112+
private DlcrEnabledHandler dlcrEnabledHandler;
113+
111114
/**
112115
* If true, then the cell renderer will be forced to be non-opaque when
113116
* used for rendering the selected item in the combo box (not in the list),
@@ -187,6 +190,7 @@ protected void installListeners() {
187190
comboBox.addPropertyChangeListener(this);
188191
comboBox.addMouseListener(buttonHandler);
189192
editorFocusHandler = new EditorFocusHandler(comboBox);
193+
dlcrEnabledHandler = new DlcrEnabledHandler(comboBox);
190194
super.installListeners();
191195
}
192196

@@ -219,6 +223,7 @@ protected void uninstallDefaults() {
219223
@Override
220224
protected void uninstallListeners() {
221225
editorFocusHandler.unregister();
226+
dlcrEnabledHandler.unregister();
222227
comboBox.removePropertyChangeListener(this);
223228
comboBox.removeMouseListener(buttonHandler);
224229
buttonHandler.pressed = false;
@@ -794,4 +799,36 @@ public void propertyChange(PropertyChangeEvent evt) {
794799
}
795800
}
796801
}
802+
803+
/**
804+
* Handler for updating combobox enabled status when renderer enabled
805+
* status changes
806+
*/
807+
private static class DlcrEnabledHandler implements PropertyChangeListener {
808+
private JComboBox<?> comboBox;
809+
810+
private DlcrEnabledHandler(JComboBox<?> comboBox) {
811+
this.comboBox = comboBox;
812+
comboBox.addPropertyChangeListener("enabled",this);
813+
}
814+
815+
public void unregister() {
816+
comboBox.removePropertyChangeListener("enabled", this);
817+
}
818+
819+
/**
820+
* Called when the combos enabled status changes
821+
*
822+
* @param evt A PropertyChangeEvent object describing the event source
823+
* and the property that has changed.
824+
*/
825+
public void propertyChange(PropertyChangeEvent evt) {
826+
if (evt.getPropertyName().equals("enabled")) {
827+
if (comboBox.getRenderer() instanceof DefaultListCellRenderer) {
828+
((DefaultListCellRenderer) comboBox.getRenderer())
829+
.setEnabled((boolean) evt.getNewValue());
830+
}
831+
}
832+
}
833+
}
797834
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 7093691
27+
* @summary Tests if JComboBox has correct font color when disabled/enabled
28+
* @run main/othervm -Dsun.java2d.uiScale=1 DisabledComboBoxFontTestAuto
29+
*/
30+
31+
import java.awt.Color;
32+
import java.awt.Graphics2D;
33+
import java.awt.image.BufferedImage;
34+
import java.io.File;
35+
import java.io.IOException;
36+
import java.nio.file.Path;
37+
38+
import javax.imageio.ImageIO;
39+
import javax.swing.DefaultListCellRenderer;
40+
import javax.swing.JComboBox;
41+
import javax.swing.SwingUtilities;
42+
import javax.swing.UIManager;
43+
import javax.swing.UnsupportedLookAndFeelException;
44+
45+
import static java.awt.image.BufferedImage.TYPE_INT_ARGB;
46+
47+
public class DisabledComboBoxFontTestAuto {
48+
private static JComboBox combo, combo2;
49+
private static BufferedImage enabledImage, disabledImage, enabledImage2, disabledImage2;
50+
private static Path testDir;
51+
private static String lafName;
52+
private static StringBuffer failingLafs;
53+
private static int COMBO_HEIGHT, COMBO_WIDTH, COMBO2_HEIGHT, COMBO2_WIDTH;
54+
55+
private static void createCombo() {
56+
combo = new JComboBox();
57+
combo.addItem("Simple JComboBox");
58+
combo.setRenderer(new DefaultListCellRenderer());
59+
combo2 = new JComboBox();
60+
combo2.addItem("Simple JComboBox");
61+
COMBO_WIDTH = (int) combo.getPreferredSize().getWidth();
62+
COMBO_HEIGHT = (int) combo.getPreferredSize().getHeight();
63+
COMBO2_WIDTH = (int) combo2.getPreferredSize().getWidth();
64+
COMBO2_HEIGHT = (int) combo2.getPreferredSize().getHeight();
65+
combo.setSize(COMBO_WIDTH, COMBO_HEIGHT);
66+
combo2.setSize(COMBO2_WIDTH, COMBO2_HEIGHT);
67+
}
68+
69+
private static void paintCombo() {
70+
combo.setEnabled(true);
71+
enabledImage = new BufferedImage(COMBO_WIDTH, COMBO_HEIGHT, TYPE_INT_ARGB);
72+
Graphics2D graphics2D = enabledImage.createGraphics();
73+
combo.paint(graphics2D);
74+
graphics2D.dispose();
75+
combo.setEnabled(false);
76+
disabledImage = new BufferedImage(COMBO_WIDTH, COMBO_HEIGHT, TYPE_INT_ARGB);
77+
graphics2D = disabledImage.createGraphics();
78+
combo.paint(graphics2D);
79+
graphics2D.dispose();
80+
combo2.setEnabled(true);
81+
enabledImage2 = new BufferedImage(COMBO2_WIDTH, COMBO2_HEIGHT, TYPE_INT_ARGB);
82+
graphics2D = enabledImage2.createGraphics();
83+
combo2.paint(graphics2D);
84+
graphics2D.dispose();
85+
combo2.setEnabled(false);
86+
disabledImage2 = new BufferedImage(COMBO2_WIDTH, COMBO2_HEIGHT, TYPE_INT_ARGB);
87+
graphics2D = disabledImage2.createGraphics();
88+
combo2.paint(graphics2D);
89+
graphics2D.dispose();
90+
}
91+
92+
private static void testMethod() throws IOException {
93+
ImageIO.write(enabledImage, "png", new File(testDir
94+
+ "/" + lafName + "Enabled.png"));
95+
ImageIO.write(disabledImage, "png", new File(testDir
96+
+ "/" + lafName + "Disabled.png"));
97+
ImageIO.write(enabledImage2, "png", new File(testDir
98+
+ "/" + lafName + "EnabledDLCR.png"));
99+
ImageIO.write(disabledImage2, "png", new File(testDir
100+
+ "/" + lafName + "DisabledDLCR.png"));
101+
102+
boolean isIdentical = true;
103+
Color eColor1, eColor2, dColor1, dColor2;
104+
105+
// Use center line to compare RGB values
106+
int y = 10;
107+
for (int x = (enabledImage.getWidth() / 2) - 20;
108+
x < (enabledImage.getWidth() / 2) + 20; x++) {
109+
// Nimbus has a pixel offset in coordinates since Nimbus is 2px
110+
// smaller in width than other L&F's
111+
if (lafName.equals("Nimbus")) {
112+
eColor1 = new Color(enabledImage.getRGB(x + 1, y));
113+
eColor2 = new Color(enabledImage2.getRGB(x, y));
114+
dColor1 = new Color(disabledImage.getRGB(x + 1, y));
115+
dColor2 = new Color(disabledImage2.getRGB(x, y));
116+
} else {
117+
eColor1 = new Color(enabledImage.getRGB(x, y));
118+
eColor2 = new Color(enabledImage2.getRGB(x, y));
119+
dColor1 = new Color(disabledImage.getRGB(x, y));
120+
dColor2 = new Color(disabledImage2.getRGB(x, y));
121+
}
122+
if ((!isColorMatching(eColor1, eColor2)) || (!isColorMatching(dColor1, dColor2))) {
123+
isIdentical = false;
124+
break;
125+
}
126+
}
127+
128+
if (isIdentical) {
129+
System.out.println("PASSED");
130+
} else {
131+
failingLafs.append(lafName + ", ");
132+
}
133+
}
134+
135+
private static boolean isColorMatching(Color c1, Color c2) {
136+
if ((c1.getRed() != c2.getRed())
137+
|| (c1.getBlue() != c2.getBlue())
138+
|| (c1.getGreen() != c2.getGreen())) {
139+
140+
System.out.println(lafName + " Enabled RGB failure: "
141+
+ c1.getRed() + ", "
142+
+ c1.getBlue() + ", "
143+
+ c1.getGreen() + " vs "
144+
+ c2.getRed() + ", "
145+
+ c2.getBlue() + ", "
146+
+ c2.getGreen());
147+
return false;
148+
}
149+
return true;
150+
}
151+
152+
private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {
153+
try {
154+
UIManager.setLookAndFeel(laf.getClassName());
155+
} catch (UnsupportedLookAndFeelException ignored){
156+
System.out.println("Unsupported LookAndFeel: " + laf.getClassName());
157+
} catch (ClassNotFoundException | InstantiationException |
158+
IllegalAccessException e) {
159+
throw new RuntimeException(e);
160+
}
161+
}
162+
163+
public static void main(String[] args) throws Exception {
164+
lafName = "null";
165+
failingLafs = new StringBuffer();
166+
testDir = Path.of(System.getProperty("test.classes", "."));
167+
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
168+
// Change Motif LAF name to avoid using slash in saved image file path
169+
lafName = laf.getName().equals("CDE/Motif") ? "Motif" : laf.getName();
170+
SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
171+
SwingUtilities.invokeAndWait(DisabledComboBoxFontTestAuto::createCombo);
172+
SwingUtilities.invokeAndWait(DisabledComboBoxFontTestAuto::paintCombo);
173+
testMethod();
174+
}
175+
if (!failingLafs.isEmpty()) {
176+
// Remove trailing comma and whitespace
177+
failingLafs.setLength(failingLafs.length() - 2);
178+
throw new RuntimeException("FAIL - Enabled and disabled ComboBox " +
179+
"does not match in these LAFs: " + failingLafs);
180+
}
181+
}
182+
}

0 commit comments

Comments
 (0)