Skip to content

Commit 4c77bd3

Browse files
committed
6529151: NullPointerException in swing.plaf.synth.SynthLookAndFeel$Handler
Reviewed-by: serb, dnguyen
1 parent d5aae01 commit 4c77bd3

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,9 @@ private void repaintIfBackgroundsDiffer(JComponent comp) {
10001000
SynthStyle style = context.getStyle();
10011001
int state = context.getComponentState();
10021002

1003+
if (style == null) {
1004+
return;
1005+
}
10031006
// Get the current background color.
10041007
Color currBG = style.getColor(context, ColorType.BACKGROUND);
10051008

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright (c) 2022, 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+
* @test
25+
* @bug 6529151
26+
* @key headful
27+
* @summary Verifies no NPE is thrown in SynthLookAndFeel$Handler
28+
* @run main RepaintNPE
29+
*/
30+
import java.awt.Container;
31+
import java.awt.Component;
32+
import java.awt.Robot;
33+
import java.awt.Point;
34+
import java.awt.event.InputEvent;
35+
36+
import javax.swing.JButton;
37+
import javax.swing.JComponent;
38+
import javax.swing.JDialog;
39+
import javax.swing.JEditorPane;
40+
import javax.swing.JFrame;
41+
import javax.swing.JScrollPane;
42+
import javax.swing.plaf.ComponentUI;
43+
import javax.swing.SwingUtilities;
44+
import javax.swing.UIManager;
45+
46+
public class RepaintNPE
47+
{
48+
private static JFrame frame;
49+
private static JButton showDialogButton = null;
50+
51+
private static class DumbDialog extends JDialog {
52+
53+
public DumbDialog ( JFrame parent )
54+
{
55+
super ( parent, "DumbDialog", true );
56+
57+
setDefaultCloseOperation ( DISPOSE_ON_CLOSE );
58+
59+
JEditorPane editorPane = new JEditorPane();
60+
editorPane.setContentType ( "text/html" );
61+
editorPane.setEditable ( false );
62+
63+
JScrollPane scrollPane = new JScrollPane ( editorPane );
64+
add( scrollPane );
65+
setSize( 400, 400 );
66+
setVisible( true );
67+
}
68+
69+
public void dispose() {
70+
super.dispose();
71+
myDispose( this );
72+
}
73+
74+
private void myDispose( Container container ) {
75+
Component[] components = container.getComponents ();
76+
77+
for ( Component comp : components ) {
78+
// Special dispose for JComponent's remove UI
79+
if (comp instanceof JComponent) {
80+
JComponent jComp = (JComponent)comp;
81+
82+
try
83+
{
84+
ComponentUI compUI = jComp.getUI();
85+
compUI.uninstallUI ( jComp );
86+
} catch ( Throwable t ) {}
87+
88+
// Recurse children
89+
myDispose( jComp );
90+
}
91+
}
92+
}
93+
94+
}
95+
96+
public static void main ( String args [] ) throws Exception
97+
{
98+
Robot robot = new Robot();
99+
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
100+
SwingUtilities.invokeAndWait ( () -> {
101+
frame = new JFrame();
102+
103+
showDialogButton = new JButton( "Show Dialog" );
104+
showDialogButton.addActionListener((e) -> {
105+
if ( e.getSource() == showDialogButton ) {
106+
new DumbDialog ( frame );
107+
}
108+
});
109+
frame.add ( showDialogButton );
110+
frame.setSize( 100, 100 );
111+
frame.setLocationRelativeTo(null);
112+
frame.setUndecorated(true);
113+
frame.setVisible( true );
114+
});
115+
robot.waitForIdle();
116+
robot.delay(1000);
117+
Point loc = frame.getLocationOnScreen();
118+
robot.mouseMove(loc.x+10, loc.y+10);
119+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
120+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
121+
robot.delay(1000);
122+
SwingUtilities.invokeAndWait(() -> frame.dispose());
123+
}
124+
}

0 commit comments

Comments
 (0)