|
| 1 | +/* |
| 2 | + * Copyright (c) 2002, 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 4546123 |
| 27 | + * @summary CardLayout becomes unusable after deleting an element |
| 28 | + * @library /java/awt/regtesthelpers |
| 29 | + * @build PassFailJFrame |
| 30 | + * @run main/manual RemoveComponentTest |
| 31 | + */ |
| 32 | + |
| 33 | +import java.awt.BorderLayout; |
| 34 | +import java.awt.CardLayout; |
| 35 | +import java.awt.Color; |
| 36 | +import java.awt.Frame; |
| 37 | +import java.awt.Insets; |
| 38 | +import java.awt.Menu; |
| 39 | +import java.awt.MenuBar; |
| 40 | +import java.awt.MenuItem; |
| 41 | +import java.awt.Panel; |
| 42 | +import java.awt.event.ActionEvent; |
| 43 | +import java.awt.event.ActionListener; |
| 44 | + |
| 45 | +import javax.swing.JLabel; |
| 46 | +import javax.swing.JPanel; |
| 47 | + |
| 48 | +public class RemoveComponentTest { |
| 49 | + public static void main(String[] args) throws Exception { |
| 50 | + String INSTRUCTIONS = """ |
| 51 | + You should see a frame titled "Test Frame For |
| 52 | + RemoveComponentTest". Try to select a few different panels from |
| 53 | + the second menu. Make sure your last choice is the red panel. |
| 54 | + Then click close (in first menu). After that you should be able |
| 55 | + to select any panels except red one. |
| 56 | + If that is the case, the test passes. Otherwise, the test failed. |
| 57 | + """; |
| 58 | + |
| 59 | + PassFailJFrame.builder() |
| 60 | + .title("Test Instructions") |
| 61 | + .instructions(INSTRUCTIONS) |
| 62 | + .columns(35) |
| 63 | + .testUI(RemoveComponentTest::createUI) |
| 64 | + .logArea(5) |
| 65 | + .build() |
| 66 | + .awaitAndCheck(); |
| 67 | + } |
| 68 | + |
| 69 | + public static Frame createUI() { |
| 70 | + TestFrame frame = new TestFrame(); |
| 71 | + frame.setSize(200, 200); |
| 72 | + return frame; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +class TestFrame extends Frame implements ActionListener { |
| 77 | + public Panel aPanel; |
| 78 | + public TestPanel pageRed; |
| 79 | + public TestPanel pageGreen; |
| 80 | + public TestPanel pageBlue; |
| 81 | + public String currentSelection = ""; |
| 82 | + |
| 83 | + public MenuItem mi; |
| 84 | + public CardLayout theCardLayout; |
| 85 | + |
| 86 | + |
| 87 | + public TestFrame() { |
| 88 | + super("Test Frame For RemoveComponentTest"); |
| 89 | + |
| 90 | + setBackground(Color.black); |
| 91 | + setLayout(new BorderLayout(5, 5)); |
| 92 | + |
| 93 | + MenuBar mb = new MenuBar(); |
| 94 | + |
| 95 | + Menu fileMenu = new Menu("File"); |
| 96 | + Menu pageMenu = new Menu("Pages"); |
| 97 | + |
| 98 | + mi = new MenuItem("Close"); |
| 99 | + mi.addActionListener(this); |
| 100 | + fileMenu.add(mi); |
| 101 | + |
| 102 | + mi = new MenuItem("Red"); |
| 103 | + mi.addActionListener(this); |
| 104 | + pageMenu.add(mi); |
| 105 | + |
| 106 | + mi = new MenuItem("Green"); |
| 107 | + mi.addActionListener(this); |
| 108 | + pageMenu.add(mi); |
| 109 | + |
| 110 | + mi = new MenuItem("Blue"); |
| 111 | + mi.addActionListener(this); |
| 112 | + pageMenu.add(mi); |
| 113 | + |
| 114 | + mb.add(fileMenu); |
| 115 | + mb.add(pageMenu); |
| 116 | + |
| 117 | + setMenuBar(mb); |
| 118 | + |
| 119 | + aPanel = new Panel(); |
| 120 | + theCardLayout = new CardLayout(); |
| 121 | + |
| 122 | + aPanel.setLayout(theCardLayout); |
| 123 | + |
| 124 | + pageRed = new TestPanel("PageRed", Color.red); |
| 125 | + pageGreen = new TestPanel("PageGreen", Color.green); |
| 126 | + pageBlue = new TestPanel("PageBlue", Color.blue); |
| 127 | + |
| 128 | + aPanel.add("PageRed", pageRed); |
| 129 | + aPanel.add("PageGreen", pageGreen); |
| 130 | + aPanel.add("PageBlue", pageBlue); |
| 131 | + |
| 132 | + add("Center", aPanel); |
| 133 | + setSize(getPreferredSize()); |
| 134 | + } |
| 135 | + |
| 136 | + public Insets getInsets() { |
| 137 | + return new Insets(47, 9, 9, 9); |
| 138 | + } |
| 139 | + |
| 140 | + public void actionPerformed(ActionEvent e) { |
| 141 | + if (e.getActionCommand().equals("Red")) { |
| 142 | + theCardLayout.show(aPanel, "PageRed"); |
| 143 | + currentSelection = "PageRed"; |
| 144 | + } else if (e.getActionCommand().equals("Green")) { |
| 145 | + theCardLayout.show(aPanel, "PageGreen"); |
| 146 | + } else if (e.getActionCommand().equals("Blue")) { |
| 147 | + theCardLayout.show(aPanel, "PageBlue"); |
| 148 | + } else if (e.getActionCommand().equals("Close")) { |
| 149 | + PassFailJFrame.log("Closing"); |
| 150 | + |
| 151 | + if (currentSelection.equals("PageRed")) { |
| 152 | + PassFailJFrame.log("Remove page red"); |
| 153 | + theCardLayout.removeLayoutComponent(pageRed); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +class TestPanel extends JPanel { |
| 160 | + private String pageName; |
| 161 | + |
| 162 | + TestPanel(String pageName, Color color) { |
| 163 | + setBackground(color); |
| 164 | + add(new JLabel(pageName)); |
| 165 | + } |
| 166 | +} |
0 commit comments