1 /*
   2  * Copyright (c) 2016, 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 javax.swing.JFrame;
  25 import javax.swing.SwingUtilities;
  26 import java.awt.Canvas;
  27 import java.awt.Color;
  28 import java.awt.Dimension;
  29 import java.awt.Frame;
  30 import java.awt.GraphicsConfiguration;
  31 import java.awt.GraphicsDevice;
  32 import java.awt.GraphicsEnvironment;
  33 import java.lang.reflect.InvocationTargetException;
  34 
  35 
  36 
  37 /* @test
  38    @bug 8160696
  39    @summary IllegalArgumentException: adding a component to a container on a different GraphicsDevice
  40    @author Mikhail Cherkasov
  41    @run main MoveToOtherScreenTest
  42    @key headful
  43 */
  44 public class MoveToOtherScreenTest {
  45 
  46     private static volatile boolean twoDisplays = true;
  47     private static final Canvas canvas = new Canvas();
  48     private static final Frame[] frms = new JFrame[2];
  49 
  50     public static void main(String[] args) throws InterruptedException, InvocationTargetException {
  51         SwingUtilities.invokeAndWait(new Runnable() {
  52             public void run() {
  53                 GraphicsEnvironment ge = GraphicsEnvironment.
  54                         getLocalGraphicsEnvironment();
  55                 GraphicsDevice[] gds = ge.getScreenDevices();
  56                 if (gds.length < 2) {
  57                     System.out.println("Test requires at least 2 displays");
  58                     twoDisplays = false;
  59                     return;
  60                 }
  61                 for (int i = 0; i < 2; i++) {
  62                     GraphicsConfiguration conf = gds[i].getConfigurations()[0];
  63                     JFrame frm = new JFrame("Frame " + i);
  64                     frm.setLocation(conf.getBounds().x, 0); // On first screen
  65                     frm.setSize(new Dimension(400, 400));
  66                     frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  67                     frm.setVisible(true);
  68                     frms[i] = frm;
  69                 }
  70                 canvas.setBackground(Color.red);
  71                 frms[0].add(canvas);
  72             }
  73         });
  74         if(!twoDisplays){
  75            return;
  76         }
  77         Thread.sleep(200);
  78         SwingUtilities.invokeAndWait(new Runnable() {
  79             @Override
  80             public void run() {
  81                 frms[1].add(canvas);
  82             }
  83         });
  84         for (Frame frm : frms) {
  85             frm.dispose();
  86         }
  87     }
  88 }