Skip to content

Commit c63209b

Browse files
author
Ichiroh Takiguchi
committed
8262085: Hovering Metal HTML Tooltips in different windows cause IllegalArgExc on Linux
Backport-of: c569f1d
1 parent 4b45646 commit c63209b

File tree

2 files changed

+87
-3
lines changed

2 files changed

+87
-3
lines changed

src/java.desktop/share/classes/javax/swing/plaf/metal/MetalToolTipUI.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2021, 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
@@ -29,8 +29,6 @@
2929
import java.awt.*;
3030
import java.awt.event.*;
3131
import javax.swing.*;
32-
import javax.swing.BorderFactory;
33-
import javax.swing.border.Border;
3432
import javax.swing.plaf.*;
3533
import javax.swing.plaf.basic.BasicToolTipUI;
3634
import javax.swing.plaf.basic.BasicHTML;
@@ -120,6 +118,11 @@ public void paint(Graphics g, JComponent c) {
120118
insets.top,
121119
size.width - (insets.left + insets.right) - 6 - accelSpacing,
122120
size.height - (insets.top + insets.bottom));
121+
122+
if (paintTextR.width <= 0 || paintTextR.height <= 0) {
123+
return;
124+
}
125+
123126
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
124127
if (v != null) {
125128
v.paint(g, paintTextR);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (c) 2021, 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+
* @key headful
27+
* @bug 8262085
28+
* @summary Tests tooltip for not throwing IllegalArgumentException on fast switching between frames.
29+
* @run main FastTooltipSwitchIAE
30+
*/
31+
32+
import javax.swing.JToolTip;
33+
import javax.swing.SwingUtilities;
34+
import javax.swing.UIManager;
35+
import javax.swing.UnsupportedLookAndFeelException;
36+
import java.awt.Dimension;
37+
import java.awt.Graphics2D;
38+
import java.awt.image.BufferedImage;
39+
40+
public class FastTooltipSwitchIAE {
41+
static Dimension oneByOneSize = new Dimension(1, 1);
42+
43+
public static void main(String[] args) {
44+
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
45+
try {
46+
SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
47+
SwingUtilities.invokeAndWait(FastTooltipSwitchIAE::doTest);
48+
System.out.println("Test passed for LookAndFeel " + laf.getClassName());
49+
} catch (Exception e) {
50+
throw new RuntimeException("Test failed for " + laf.getClassName(), e);
51+
}
52+
}
53+
}
54+
55+
private static void setLookAndFeel(final UIManager.LookAndFeelInfo laf) {
56+
try {
57+
System.out.println("LookAndFeel: " + laf.getClassName());
58+
UIManager.setLookAndFeel(laf.getClassName());
59+
} catch (UnsupportedLookAndFeelException ignored) {
60+
System.err.println("Unsupported L&F: " + laf.getClassName());
61+
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
62+
throw new RuntimeException(e);
63+
}
64+
}
65+
66+
private static void doTest() {
67+
JToolTip toolTip = new JToolTip();
68+
toolTip.setTipText("<html><h1>Hello world</h1></html>");
69+
toolTip.setMinimumSize(oneByOneSize);
70+
toolTip.setMaximumSize(oneByOneSize);
71+
toolTip.setPreferredSize(oneByOneSize);
72+
toolTip.setBounds(100, 100, 1, 1);
73+
74+
BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
75+
Graphics2D g2d = img.createGraphics();
76+
77+
toolTip.paint(g2d);
78+
79+
g2d.dispose();
80+
}
81+
}

0 commit comments

Comments
 (0)