|
| 1 | +/* |
| 2 | + * Copyright (c) 1999, 2023, 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 4196100 |
| 27 | + @summary Make sure findComponentAt() only returns visible components. |
| 28 | + @key headful |
| 29 | +*/ |
| 30 | + |
| 31 | +import java.awt.BorderLayout; |
| 32 | +import java.awt.Color; |
| 33 | +import java.awt.Dimension; |
| 34 | +import java.awt.EventQueue; |
| 35 | +import javax.swing.JFrame; |
| 36 | +import javax.swing.JTabbedPane; |
| 37 | +import javax.swing.JPanel; |
| 38 | + |
| 39 | +public class FindComponentTest { |
| 40 | + |
| 41 | + public static void main(String[] args) throws Exception { |
| 42 | + EventQueue.invokeAndWait(() -> { |
| 43 | + FindComponentFrame findComponentAtTest = new FindComponentFrame(); |
| 44 | + |
| 45 | + try { |
| 46 | + if (!findComponentAtTest.didItWork()) { |
| 47 | + throw new RuntimeException( |
| 48 | + "findComponentAt() returned non-visible component"); |
| 49 | + } |
| 50 | + } finally { |
| 51 | + findComponentAtTest.dispose(); |
| 52 | + } |
| 53 | + }); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +class FindComponentFrame extends JFrame { |
| 59 | + public FindComponentFrame() { |
| 60 | + super("FindComponentFrame"); |
| 61 | + } |
| 62 | + |
| 63 | + public boolean didItWork() { |
| 64 | + setTitle("FindComponentTest"); |
| 65 | + setSize(new Dimension(200, 200)); |
| 66 | + |
| 67 | + JTabbedPane tabbedpane = new JTabbedPane(); |
| 68 | + setContentPane(tabbedpane); |
| 69 | + |
| 70 | + JPanel panel1 = new JPanel(); |
| 71 | + panel1.setName("Panel 1"); |
| 72 | + panel1.setLayout(new BorderLayout()); |
| 73 | + tabbedpane.add(panel1); |
| 74 | + JPanel subPanel = new JPanel(); |
| 75 | + subPanel.setName("Sub-Panel"); |
| 76 | + subPanel.setBackground(Color.green); |
| 77 | + panel1.add(subPanel); // add sub panel to 1st tab |
| 78 | + |
| 79 | + JPanel panel2 = new JPanel(); |
| 80 | + panel2.setName("Panel 2"); |
| 81 | + tabbedpane.add(panel2); |
| 82 | + |
| 83 | + tabbedpane.setSelectedIndex(1); // display 2nd tab |
| 84 | + setVisible(true); |
| 85 | + |
| 86 | + boolean success = tabbedpane.findComponentAt(50,50) |
| 87 | + .getName().equals("Panel 2"); |
| 88 | + return success; |
| 89 | + } |
| 90 | +} |
0 commit comments