|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, 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 | +/* @test |
| 25 | + @bug 8255880 |
| 26 | + @key headful |
| 27 | + @summary Swing components, whose internal state changed while a frame was |
| 28 | + iconified, are not redrawn after the frame becomes deiconified. |
| 29 | + */ |
| 30 | + |
| 31 | +import java.awt.AWTException; |
| 32 | +import java.awt.Container; |
| 33 | +import java.awt.Dimension; |
| 34 | +import java.awt.FlowLayout; |
| 35 | +import java.awt.Graphics; |
| 36 | +import java.awt.Robot; |
| 37 | +import java.awt.Toolkit; |
| 38 | +import java.lang.reflect.InvocationTargetException; |
| 39 | +import javax.swing.JButton; |
| 40 | +import javax.swing.JComponent; |
| 41 | +import javax.swing.JFrame; |
| 42 | +import javax.swing.JLabel; |
| 43 | +import javax.swing.SwingUtilities; |
| 44 | +import javax.swing.UIManager; |
| 45 | +import javax.swing.UnsupportedLookAndFeelException; |
| 46 | +import javax.swing.plaf.metal.MetalLookAndFeel; |
| 47 | + |
| 48 | +public class RepaintOnFrameIconifiedStateChangeTest { |
| 49 | + private static final String[][] strsForComps = new String[][] { |
| 50 | + {"JLabel AAA", "JLabel BBB"}, |
| 51 | + {"JButton AAA", "JButton BBB"}}; |
| 52 | + private static final int lblIndex = 0; |
| 53 | + private static final int btnIndex = 1; |
| 54 | + |
| 55 | + private static volatile JFrame frame; |
| 56 | + private static volatile JLabel label; |
| 57 | + private static volatile JButton button; |
| 58 | + private static volatile JComponent[] comps = new JComponent[2]; |
| 59 | + private static volatile boolean[] compRedrawn = new boolean[2]; |
| 60 | + private static volatile boolean compRedrawnFlagCanBeSet = false; |
| 61 | + |
| 62 | + public static void main(String[] args) { |
| 63 | + Toolkit toolkit = Toolkit.getDefaultToolkit(); |
| 64 | + if (!toolkit.isFrameStateSupported(JFrame.ICONIFIED) || |
| 65 | + !toolkit.isFrameStateSupported(JFrame.NORMAL)) { |
| 66 | + System.out.println("ICONIFIED or NORMAL frame states are not" + |
| 67 | + "supported by a toolkit."); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + try { |
| 72 | + SwingUtilities.invokeAndWait(new Runnable() { |
| 73 | + @Override |
| 74 | + public void run() { |
| 75 | + System.out.println("Creating GUI..."); |
| 76 | + createGUI(); |
| 77 | + } |
| 78 | + }); |
| 79 | + Robot robot = new Robot(); |
| 80 | + robot.delay(2000); |
| 81 | + |
| 82 | + SwingUtilities.invokeAndWait(new Runnable() { |
| 83 | + @Override |
| 84 | + public void run() { |
| 85 | + System.out.println("Minimizing the frame..."); |
| 86 | + frame.setExtendedState(JFrame.ICONIFIED); |
| 87 | + } |
| 88 | + }); |
| 89 | + robot.delay(2000); |
| 90 | + |
| 91 | + SwingUtilities.invokeAndWait(new Runnable() { |
| 92 | + @Override |
| 93 | + public void run() { |
| 94 | + System.out.println("Changing states of components..."); |
| 95 | + label.setText(strsForComps[lblIndex][1]); |
| 96 | + button.setText(strsForComps[btnIndex][1]); |
| 97 | + } |
| 98 | + }); |
| 99 | + robot.delay(2000); |
| 100 | + |
| 101 | + SwingUtilities.invokeAndWait(new Runnable() { |
| 102 | + @Override |
| 103 | + public void run() { |
| 104 | + System.out.println("Restoring the frame..."); |
| 105 | + for (int i = 0; i < compRedrawn.length; i++) { |
| 106 | + compRedrawn[i] = false; |
| 107 | + } |
| 108 | + compRedrawnFlagCanBeSet = true; |
| 109 | + |
| 110 | + frame.setExtendedState(JFrame.NORMAL); |
| 111 | + frame.toFront(); |
| 112 | + } |
| 113 | + }); |
| 114 | + robot.delay(2000); |
| 115 | + |
| 116 | + int notRedrawnCompsCount = 0; |
| 117 | + for (int i = 0; i < compRedrawn.length; i++) { |
| 118 | + if (!compRedrawn[i]) { |
| 119 | + notRedrawnCompsCount++; |
| 120 | + System.out.println(String.format( |
| 121 | + "Not redrawn component #%d: '%s'", i, comps[i])); |
| 122 | + } |
| 123 | + } |
| 124 | + if (notRedrawnCompsCount > 0) { |
| 125 | + throw new RuntimeException(String.format( |
| 126 | + "'%d' components were not redrawn.", |
| 127 | + notRedrawnCompsCount)); |
| 128 | + } |
| 129 | + System.out.println("Test passed."); |
| 130 | + } catch (InterruptedException | InvocationTargetException | |
| 131 | + AWTException e) { |
| 132 | + throw new RuntimeException(e); |
| 133 | + } finally { |
| 134 | + try { |
| 135 | + SwingUtilities.invokeAndWait(new Runnable() { |
| 136 | + @Override |
| 137 | + public void run() { |
| 138 | + if (frame != null) { |
| 139 | + frame.dispose(); |
| 140 | + frame = null; |
| 141 | + } |
| 142 | + } |
| 143 | + }); |
| 144 | + } catch (InterruptedException | InvocationTargetException e) { |
| 145 | + throw new RuntimeException(e); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + private static void createGUI() { |
| 151 | + if (!(UIManager.getLookAndFeel() instanceof MetalLookAndFeel)) { |
| 152 | + try { |
| 153 | + UIManager.setLookAndFeel(new MetalLookAndFeel()); |
| 154 | + } catch (UnsupportedLookAndFeelException ulafe) { |
| 155 | + throw new RuntimeException(ulafe); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + frame = new JFrame("RepaintOnFrameIconifiedStateChangeTest"); |
| 160 | + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); |
| 161 | + Container content = frame.getContentPane(); |
| 162 | + content.setLayout(new FlowLayout()); |
| 163 | + |
| 164 | + comps[lblIndex] = label = new JLabel(strsForComps[lblIndex][0]) { |
| 165 | + @Override |
| 166 | + public void paint(Graphics g) { |
| 167 | + super.paint(g); |
| 168 | + if (compRedrawnFlagCanBeSet) { |
| 169 | + compRedrawn[lblIndex] = true; |
| 170 | + } |
| 171 | + } |
| 172 | + }; |
| 173 | + label.setPreferredSize(new Dimension(150, 50)); |
| 174 | + content.add(label); |
| 175 | + |
| 176 | + comps[btnIndex] = button = new JButton(strsForComps[btnIndex][0]) { |
| 177 | + @Override |
| 178 | + public void paint(Graphics g) { |
| 179 | + super.paint(g); |
| 180 | + if (compRedrawnFlagCanBeSet) { |
| 181 | + compRedrawn[btnIndex] = true; |
| 182 | + } |
| 183 | + } |
| 184 | + }; |
| 185 | + button.setPreferredSize(new Dimension(200, 50)); |
| 186 | + button.setFocusable(false); |
| 187 | + content.add(button); |
| 188 | + |
| 189 | + frame.pack(); |
| 190 | + frame.setVisible(true); |
| 191 | + } |
| 192 | +} |
0 commit comments