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