Skip to content

Commit 3b1062d

Browse files
author
Alexander Zuev
committed
8322239: [macos] a11y : java.lang.NullPointerException is thrown when focus is moved on the JTabbedPane
Reviewed-by: asemenov, abhiscxk, aivanov
1 parent 5a988a5 commit 3b1062d

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, 2021, 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
@@ -31,6 +31,7 @@
3131
import java.awt.Dimension;
3232
import java.awt.Font;
3333
import java.awt.FontMetrics;
34+
import java.awt.IllegalComponentStateException;
3435
import java.awt.Point;
3536
import java.awt.Rectangle;
3637
import java.awt.event.FocusListener;
@@ -2337,15 +2338,23 @@ public boolean contains(Point p) {
23372338
}
23382339

23392340
public Point getLocationOnScreen() {
2340-
Point parentLocation = parent.getLocationOnScreen();
2341+
Point parentLocation;
2342+
try {
2343+
parentLocation = parent.getLocationOnScreen();
2344+
} catch (IllegalComponentStateException icse) {
2345+
return null;
2346+
}
23412347
Point componentLocation = getLocation();
2348+
if (parentLocation == null || componentLocation == null) {
2349+
return null;
2350+
}
23422351
componentLocation.translate(parentLocation.x, parentLocation.y);
23432352
return componentLocation;
23442353
}
23452354

23462355
public Point getLocation() {
23472356
Rectangle r = getBounds();
2348-
return new Point(r.x, r.y);
2357+
return r == null ? null : new Point(r.x, r.y);
23492358
}
23502359

23512360
public void setLocation(Point p) {
@@ -2362,7 +2371,7 @@ public void setBounds(Rectangle r) {
23622371

23632372
public Dimension getSize() {
23642373
Rectangle r = getBounds();
2365-
return new Dimension(r.width, r.height);
2374+
return r == null ? null : new Dimension(r.width, r.height);
23662375
}
23672376

23682377
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)