Path: blob/master/test/jdk/javax/swing/JFrame/8255880/RepaintOnFrameIconifiedStateChangeTest.java
41153 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@bug 825588025@key headful26@summary Swing components, whose internal state changed while a frame was27iconified, are not redrawn after the frame becomes deiconified.28*/2930import java.awt.AWTException;31import java.awt.Container;32import java.awt.Dimension;33import java.awt.FlowLayout;34import java.awt.Graphics;35import java.awt.Robot;36import java.awt.Toolkit;37import java.lang.reflect.InvocationTargetException;38import javax.swing.JButton;39import javax.swing.JComponent;40import javax.swing.JFrame;41import javax.swing.JLabel;42import javax.swing.SwingUtilities;43import javax.swing.UIManager;44import javax.swing.UnsupportedLookAndFeelException;45import javax.swing.plaf.metal.MetalLookAndFeel;4647public class RepaintOnFrameIconifiedStateChangeTest {48private static final String[][] strsForComps = new String[][] {49{"JLabel AAA", "JLabel BBB"},50{"JButton AAA", "JButton BBB"}};51private static final int lblIndex = 0;52private static final int btnIndex = 1;5354private static volatile JFrame frame;55private static volatile JLabel label;56private static volatile JButton button;57private static volatile JComponent[] comps = new JComponent[2];58private static volatile boolean[] compRedrawn = new boolean[2];59private static volatile boolean compRedrawnFlagCanBeSet = false;6061public static void main(String[] args) {62Toolkit toolkit = Toolkit.getDefaultToolkit();63if (!toolkit.isFrameStateSupported(JFrame.ICONIFIED) ||64!toolkit.isFrameStateSupported(JFrame.NORMAL)) {65System.out.println("ICONIFIED or NORMAL frame states are not" +66"supported by a toolkit.");67return;68}6970try {71SwingUtilities.invokeAndWait(new Runnable() {72@Override73public void run() {74System.out.println("Creating GUI...");75createGUI();76}77});78Robot robot = new Robot();79robot.delay(2000);8081SwingUtilities.invokeAndWait(new Runnable() {82@Override83public void run() {84System.out.println("Minimizing the frame...");85frame.setExtendedState(JFrame.ICONIFIED);86}87});88robot.delay(2000);8990SwingUtilities.invokeAndWait(new Runnable() {91@Override92public void run() {93System.out.println("Changing states of components...");94label.setText(strsForComps[lblIndex][1]);95button.setText(strsForComps[btnIndex][1]);96}97});98robot.delay(2000);99100SwingUtilities.invokeAndWait(new Runnable() {101@Override102public void run() {103System.out.println("Restoring the frame...");104for (int i = 0; i < compRedrawn.length; i++) {105compRedrawn[i] = false;106}107compRedrawnFlagCanBeSet = true;108109frame.setExtendedState(JFrame.NORMAL);110frame.toFront();111}112});113robot.delay(2000);114115int notRedrawnCompsCount = 0;116for (int i = 0; i < compRedrawn.length; i++) {117if (!compRedrawn[i]) {118notRedrawnCompsCount++;119System.out.println(String.format(120"Not redrawn component #%d: '%s'", i, comps[i]));121}122}123if (notRedrawnCompsCount > 0) {124throw new RuntimeException(String.format(125"'%d' components were not redrawn.",126notRedrawnCompsCount));127}128System.out.println("Test passed.");129} catch (InterruptedException | InvocationTargetException |130AWTException e) {131throw new RuntimeException(e);132} finally {133try {134SwingUtilities.invokeAndWait(new Runnable() {135@Override136public void run() {137if (frame != null) {138frame.dispose();139frame = null;140}141}142});143} catch (InterruptedException | InvocationTargetException e) {144throw new RuntimeException(e);145}146}147}148149private static void createGUI() {150if (!(UIManager.getLookAndFeel() instanceof MetalLookAndFeel)) {151try {152UIManager.setLookAndFeel(new MetalLookAndFeel());153} catch (UnsupportedLookAndFeelException ulafe) {154throw new RuntimeException(ulafe);155}156}157158frame = new JFrame("RepaintOnFrameIconifiedStateChangeTest");159frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);160Container content = frame.getContentPane();161content.setLayout(new FlowLayout());162163comps[lblIndex] = label = new JLabel(strsForComps[lblIndex][0]) {164@Override165public void paint(Graphics g) {166super.paint(g);167if (compRedrawnFlagCanBeSet) {168compRedrawn[lblIndex] = true;169}170}171};172label.setPreferredSize(new Dimension(150, 50));173content.add(label);174175comps[btnIndex] = button = new JButton(strsForComps[btnIndex][0]) {176@Override177public void paint(Graphics g) {178super.paint(g);179if (compRedrawnFlagCanBeSet) {180compRedrawn[btnIndex] = true;181}182}183};184button.setPreferredSize(new Dimension(200, 50));185button.setFocusable(false);186content.add(button);187188frame.pack();189frame.setVisible(true);190}191}192193194