Skip to content

Commit ccbe8fa

Browse files
DamonGuyprrace
authored andcommitted
8282772: JButton text set as HTML content has unwanted padding
Reviewed-by: prr, psadhukhan
1 parent 1586bf8 commit ccbe8fa

File tree

3 files changed

+111
-36
lines changed

3 files changed

+111
-36
lines changed

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

+37-11
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,40 @@
2525

2626
package javax.swing.plaf.basic;
2727

28-
import sun.swing.SwingUtilities2;
2928
import sun.awt.AppContext;
30-
31-
import java.awt.*;
32-
import java.awt.event.*;
33-
import java.io.Serializable;
34-
import javax.swing.*;
35-
import javax.swing.border.*;
36-
import java.awt.*;
37-
import java.awt.event.*;
29+
import sun.swing.SwingUtilities2;
30+
import java.awt.AWTKeyStroke;
31+
import java.awt.Component;
32+
import java.awt.Dimension;
33+
import java.awt.FontMetrics;
34+
import java.awt.Graphics;
35+
import java.awt.Insets;
36+
import java.awt.KeyboardFocusManager;
37+
import java.awt.Rectangle;
38+
import java.awt.event.ActionEvent;
39+
import java.awt.event.KeyEvent;
40+
import java.awt.event.KeyListener;
41+
import java.awt.event.MouseMotionListener;
3842
import java.util.Enumeration;
3943
import java.util.HashSet;
4044
import java.util.Set;
45+
46+
import javax.swing.AbstractAction;
47+
import javax.swing.AbstractButton;
48+
import javax.swing.ButtonGroup;
49+
import javax.swing.ButtonModel;
50+
import javax.swing.DefaultButtonModel;
51+
import javax.swing.Icon;
52+
import javax.swing.JComponent;
53+
import javax.swing.JRadioButton;
54+
import javax.swing.JToggleButton;
55+
import javax.swing.KeyStroke;
56+
import javax.swing.LookAndFeel;
57+
import javax.swing.SwingUtilities;
58+
import javax.swing.UIManager;
4159
import javax.swing.plaf.ButtonUI;
42-
import javax.swing.plaf.UIResource;
4360
import javax.swing.plaf.ComponentUI;
61+
import javax.swing.plaf.UIResource;
4462
import javax.swing.text.View;
4563

4664
/**
@@ -580,7 +598,15 @@ public Component.BaselineResizeBehavior getBaselineResizeBehavior(
580598

581599
private String layout(AbstractButton b, FontMetrics fm,
582600
int width, int height) {
583-
Insets i = b.getInsets();
601+
Insets i;
602+
603+
final View v = (View)b.getClientProperty(BasicHTML.propertyKey);
604+
if (v != null) {
605+
i = new Insets(0, 0, 0, 0);
606+
} else {
607+
i = b.getInsets();
608+
}
609+
584610
viewRect.x = i.left;
585611
viewRect.y = i.top;
586612
viewRect.width = width - (i.right + viewRect.x);

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

+30-11
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,25 @@
2424
*/
2525
package javax.swing.plaf.synth;
2626

27-
import sun.swing.SwingUtilities2;
2827
import sun.swing.MenuItemLayoutHelper;
29-
30-
import java.awt.*;
31-
import javax.swing.*;
28+
import sun.swing.SwingUtilities2;
29+
import java.awt.Color;
30+
import java.awt.Component;
31+
import java.awt.Dimension;
32+
import java.awt.Font;
33+
import java.awt.FontMetrics;
34+
import java.awt.Graphics;
35+
import java.awt.Insets;
36+
import java.awt.Rectangle;
37+
38+
import javax.swing.ButtonModel;
39+
import javax.swing.Icon;
40+
import javax.swing.JButton;
41+
import javax.swing.JComponent;
42+
import javax.swing.JMenuItem;
43+
import javax.swing.SwingUtilities;
3244
import javax.swing.plaf.basic.BasicHTML;
33-
import javax.swing.text.*;
45+
import javax.swing.text.View;
3446

3547
/**
3648
* Wrapper for primitive graphics calls.
@@ -380,10 +392,19 @@ public void paintText(SynthContext ss, Graphics g, String text,
380392
FontMetrics fm = SwingUtilities2.getFontMetrics(c, g);
381393
Insets insets = SynthLookAndFeel.getPaintingInsets(ss, paintInsets);
382394

383-
paintViewR.x = insets.left;
384-
paintViewR.y = insets.top;
385-
paintViewR.width = c.getWidth() - (insets.left + insets.right);
386-
paintViewR.height = c.getHeight() - (insets.top + insets.bottom);
395+
final View v = (View)c.getClientProperty(BasicHTML.propertyKey);
396+
397+
if (c instanceof JButton && v != null) {
398+
paintViewR.x = 0;
399+
paintViewR.y = 0;
400+
paintViewR.width = c.getWidth();
401+
paintViewR.height = c.getHeight();
402+
} else {
403+
paintViewR.x = insets.left;
404+
paintViewR.y = insets.top;
405+
paintViewR.width = c.getWidth() - (insets.left + insets.right);
406+
paintViewR.height = c.getHeight() - (insets.top + insets.bottom);
407+
}
387408

388409
paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
389410
paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;
@@ -409,8 +430,6 @@ public void paintText(SynthContext ss, Graphics g, String text,
409430
}
410431

411432
if (text != null) {
412-
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
413-
414433
if (v != null) {
415434
v.paint(g, paintTextR);
416435
} else {

test/jdk/javax/swing/JButton/HtmlButtonImageTest/HtmlButtonImageTest.java

+44-14
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
/*
2525
* @test
2626
* @bug 8015854
27-
* @requires (os.family == "mac")
28-
* @summary Tests HTML image as JButton text for unwanted padding on macOS Aqua LAF
27+
* @summary Tests HTML image as JButton text for unwanted padding
2928
* @run main HtmlButtonImageTest
3029
*/
3130

@@ -41,6 +40,7 @@
4140
import javax.swing.JButton;
4241
import javax.swing.SwingUtilities;
4342
import javax.swing.UIManager;
43+
import javax.swing.UnsupportedLookAndFeelException;
4444

4545
import static java.awt.image.BufferedImage.TYPE_INT_ARGB;
4646

@@ -60,19 +60,38 @@ public final class HtmlButtonImageTest {
6060
private static final int maxX = centerX + (SQUARE_WIDTH / 2);
6161
private static final int maxY = centerY + (SQUARE_HEIGHT / 2);
6262

63+
private static boolean supportedLaf;
64+
private static int failCount = 0;
65+
private static String currentLaf = new String();
66+
private static StringBuffer failedLafs = new StringBuffer();
67+
68+
6369
public static void main(String[] args) throws Exception {
64-
UIManager.setLookAndFeel("com.apple.laf.AquaLookAndFeel");
6570
testDir = Path.of(System.getProperty("test.classes", "."));
6671
generateRedSquare();
6772

68-
SwingUtilities.invokeAndWait(HtmlButtonImageTest::createButton);
69-
SwingUtilities.invokeAndWait(HtmlButtonImageTest::paintButton);
73+
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
74+
SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
75+
if(supportedLaf) {
76+
currentLaf = laf.getName();
77+
SwingUtilities.invokeAndWait(HtmlButtonImageTest::createButton);
78+
SwingUtilities.invokeAndWait(HtmlButtonImageTest::paintButton);
79+
80+
testImageCentering(image.getRGB(centerX, centerY),
81+
image.getRGB(minX, minY),
82+
image.getRGB(minX, maxY),
83+
image.getRGB(maxX, minY),
84+
image.getRGB(maxX, maxY));
85+
}
86+
}
7087

71-
testImageCentering(image.getRGB(centerX, centerY),
72-
image.getRGB(minX, minY),
73-
image.getRGB(minX, maxY),
74-
image.getRGB(maxX, minY),
75-
image.getRGB(maxX, maxY));
88+
if(!failedLafs.isEmpty()) {
89+
if(failCount > 1) {
90+
failedLafs.setLength(failedLafs.length() - 2);
91+
}
92+
throw new RuntimeException("HTML image not centered in button " +
93+
"for these L&F's: " + failedLafs);
94+
}
7695
}
7796

7897
private static void generateRedSquare() throws IOException {
@@ -105,11 +124,22 @@ private static boolean checkRedColor(int rgb) {
105124
private static void testImageCentering(int... colors) throws IOException {
106125
for (int c : colors) {
107126
if (!checkRedColor(c)) {
108-
ImageIO.write(image, "png",
109-
new File(testDir + "/fail_image.png"));
110-
throw new RuntimeException("HTML image not centered in button");
127+
failCount++;
128+
ImageIO.write(image, "png", new File(testDir + "/fail_image_" +
129+
currentLaf.replaceAll("[^\\w\\s]","") + ".png"));
130+
failedLafs.append(currentLaf + ", ");
131+
break;
111132
}
112133
}
113-
System.out.println("Passed");
134+
}
135+
136+
private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {
137+
try {
138+
UIManager.setLookAndFeel(laf.getClassName());
139+
supportedLaf = true;
140+
} catch (UnsupportedLookAndFeelException | ClassNotFoundException |
141+
InstantiationException | IllegalAccessException e) {
142+
supportedLaf = false;
143+
}
114144
}
115145
}

0 commit comments

Comments
 (0)