Skip to content

Commit 891df21

Browse files
Harshitha Onkarprsadhuk
Harshitha Onkar
authored andcommittedAug 8, 2022
8259687: JTabbedPane.setComponentAt doesn't hide previously visible tab component
Reviewed-by: psadhukhan, dnguyen
1 parent d4fb91b commit 891df21

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
 

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

+5
Original file line numberDiff line numberDiff line change
@@ -1598,6 +1598,11 @@ public void setComponentAt(int index, Component component) {
15981598
boolean selectedPage = (getSelectedIndex() == index);
15991599

16001600
if (selectedPage) {
1601+
if (this.visComp != null && this.visComp.isVisible()
1602+
&& !this.visComp.equals(component)) {
1603+
// previous component visibility is set to false
1604+
this.visComp.setVisible(false);
1605+
}
16011606
this.visComp = component;
16021607
}
16031608

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
import java.awt.AWTException;
25+
import java.awt.Color;
26+
import java.awt.Robot;
27+
import java.lang.reflect.InvocationTargetException;
28+
import javax.swing.JFrame;
29+
import javax.swing.JPanel;
30+
import javax.swing.JTabbedPane;
31+
import javax.swing.SwingUtilities;
32+
33+
/*
34+
* @test
35+
* @bug 8259687
36+
* @key headful
37+
* @summary Tests whether the selected tab's component is on the top and visible.
38+
* @run main TabbedPaneBug
39+
*/
40+
public class TabbedPaneBug {
41+
42+
private static final int FIRST = 0;
43+
private static final int SECOND = 1;
44+
private static JFrame window;
45+
private static JTabbedPane tabs;
46+
private static final JPanel firstTab = new JPanel();
47+
private static final JPanel secondTab = new JPanel();
48+
49+
public static void main(String[] args) throws AWTException,
50+
InterruptedException, InvocationTargetException {
51+
try {
52+
Robot robot = new Robot();
53+
robot.setAutoDelay(200);
54+
SwingUtilities.invokeAndWait(() -> createAndShowGUI());
55+
56+
robot.waitForIdle();
57+
robot.mouseMove(window.getWidth()/2, window.getHeight()/2);
58+
robot.delay(500);
59+
Color actualColor = robot.getPixelColor(window.getWidth()/2,
60+
window.getHeight()/2);
61+
robot.delay(200);
62+
63+
if (actualColor.equals(Color.RED)) {
64+
throw new RuntimeException("The second (selected) component" +
65+
" is not on the top!!");
66+
}
67+
} finally {
68+
SwingUtilities.invokeAndWait(() -> {
69+
if (window != null) {
70+
window.dispose();
71+
}
72+
});
73+
}
74+
75+
}
76+
77+
private static void createAndShowGUI() {
78+
window = new JFrame("Tabbed Pane Bug");
79+
tabs = new JTabbedPane();
80+
81+
firstTab.setBackground(Color.RED);
82+
secondTab.setBackground(Color.GREEN);
83+
84+
tabs.addChangeListener((e) -> {
85+
if (tabs.getSelectedComponent() == null) {
86+
switch (tabs.getSelectedIndex()) {
87+
case FIRST:
88+
tabs.setComponentAt(tabs.getSelectedIndex(), firstTab);
89+
break;
90+
case SECOND:
91+
tabs.setComponentAt(tabs.getSelectedIndex(), secondTab);
92+
break;
93+
default:
94+
}
95+
}
96+
});
97+
98+
tabs.addTab("First", null);
99+
tabs.addTab("Second", null);
100+
101+
tabs.setSelectedIndex(SECOND);
102+
window.add(tabs);
103+
window.setSize(200, 200);
104+
window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
105+
window.setVisible(true);
106+
}
107+
}

0 commit comments

Comments
 (0)