Skip to content

Commit d1bdb25

Browse files
committed
8322239: [macos] a11y : java.lang.NullPointerException is thrown when focus is moved on the JTabbedPane
Backport-of: 3b1062d45df69d4cf8479c6a65602bd2453ab885
1 parent 2921ad6 commit d1bdb25

File tree

2 files changed

+115
-4
lines changed

2 files changed

+115
-4
lines changed

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

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2024, 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,6 +26,7 @@
2626

2727
import java.awt.*;
2828
import java.awt.event.*;
29+
import java.awt.IllegalComponentStateException;
2930
import java.beans.JavaBean;
3031
import java.beans.BeanProperty;
3132
import java.beans.Transient;
@@ -2255,15 +2256,23 @@ public boolean contains(Point p) {
22552256
}
22562257

22572258
public Point getLocationOnScreen() {
2258-
Point parentLocation = parent.getLocationOnScreen();
2259+
Point parentLocation;
2260+
try {
2261+
parentLocation = parent.getLocationOnScreen();
2262+
} catch (IllegalComponentStateException icse) {
2263+
return null;
2264+
}
22592265
Point componentLocation = getLocation();
2266+
if (parentLocation == null || componentLocation == null) {
2267+
return null;
2268+
}
22602269
componentLocation.translate(parentLocation.x, parentLocation.y);
22612270
return componentLocation;
22622271
}
22632272

22642273
public Point getLocation() {
22652274
Rectangle r = getBounds();
2266-
return new Point(r.x, r.y);
2275+
return r == null ? null : new Point(r.x, r.y);
22672276
}
22682277

22692278
public void setLocation(Point p) {
@@ -2280,7 +2289,7 @@ public void setBounds(Rectangle r) {
22802289

22812290
public Dimension getSize() {
22822291
Rectangle r = getBounds();
2283-
return new Dimension(r.width, r.height);
2292+
return r == null ? null : new Dimension(r.width, r.height);
22842293
}
22852294

22862295
public void setSize(Dimension d) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright (c) 2024, 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 8322239
27+
* @summary [macos] a11y : java.lang.NullPointerException is thrown when
28+
* focus is moved on the JTabbedPane
29+
* @key headful
30+
* @run main TabbedPaneNPECheck
31+
*/
32+
33+
import java.awt.BorderLayout;
34+
import java.awt.Dimension;
35+
import java.awt.Point;
36+
import java.awt.Rectangle;
37+
import java.lang.reflect.InvocationTargetException;
38+
import javax.accessibility.Accessible;
39+
import javax.accessibility.AccessibleComponent;
40+
import javax.accessibility.AccessibleContext;
41+
import javax.swing.JFrame;
42+
import javax.swing.JPanel;
43+
import javax.swing.JTabbedPane;
44+
import javax.swing.SwingUtilities;
45+
46+
public class TabbedPaneNPECheck {
47+
JTabbedPane pane;
48+
JFrame mainFrame;
49+
public static void main(String[] args) throws InterruptedException,
50+
InvocationTargetException {
51+
TabbedPaneNPECheck me = new TabbedPaneNPECheck();
52+
SwingUtilities.invokeAndWait(me::setupGUI);
53+
try {
54+
SwingUtilities.invokeAndWait(me::test);
55+
} finally {
56+
SwingUtilities.invokeAndWait(me::shutdownGUI);
57+
}
58+
}
59+
60+
public void setupGUI() {
61+
mainFrame = new JFrame("TabbedPaneNPECheck");
62+
pane = new JTabbedPane();
63+
Dimension panelSize = new Dimension(200, 200);
64+
for (int i = 0; i < 25; i++) {
65+
JPanel p = new JPanel();
66+
p.setMinimumSize(panelSize);
67+
p.setMaximumSize(panelSize);
68+
p.setSize(panelSize);
69+
pane.addTab("Tab no." + i, p);
70+
}
71+
mainFrame.setLayout(new BorderLayout());
72+
mainFrame.add(pane, BorderLayout.CENTER);
73+
mainFrame.setLocationRelativeTo(null);
74+
mainFrame.setSize(250, 250);
75+
mainFrame.setVisible(true);
76+
}
77+
78+
public void test() {
79+
AccessibleContext context = pane.getAccessibleContext();
80+
int nChild = context.getAccessibleChildrenCount();
81+
for (int i = 0; i < nChild; i++) {
82+
Accessible accessible = context.getAccessibleChild(i);
83+
if (accessible instanceof AccessibleComponent) {
84+
try {
85+
AccessibleComponent component = (AccessibleComponent) accessible;
86+
Point p = component.getLocationOnScreen();
87+
Rectangle r = component.getBounds();
88+
} catch (NullPointerException npe) {
89+
throw new RuntimeException("Unexpected NullPointerException " +
90+
"while getting accessible component bounds: ", npe);
91+
}
92+
}
93+
}
94+
}
95+
96+
public void shutdownGUI() {
97+
if (mainFrame != null) {
98+
mainFrame.setVisible(false);
99+
mainFrame.dispose();
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)