1 /*
   2  * Copyright (c) 2008, 2013, 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 java.applet.Applet;
  25 import java.awt.*;
  26 import java.awt.event.*;
  27 
  28 public class AppletInitialFocusTest1 extends Applet implements FocusListener {
  29 
  30     Button button1 = new Button("Button1");
  31     Button button2 = new Button("Button2");
  32 
  33     Object lock = new Object();
  34 
  35     public void init() {
  36 
  37         Component parent = this;
  38         while (parent != null && !(parent instanceof Window)) {
  39             parent = parent.getParent();
  40         }
  41         /*
  42          * This applet is designed to be run only with appletviewer,
  43          * so there always should be a toplevel frame.
  44          */
  45         if (parent == null) {
  46             synchronized (lock) {
  47                 System.err.println("appletviewer not running");
  48                 System.exit(3);
  49             }
  50         }
  51         button1.addFocusListener(this);
  52         button2.addFocusListener(this);
  53         add(button1);
  54         add(button2);
  55         button2.requestFocus();
  56     }
  57 
  58     public void focusGained(FocusEvent e) {
  59         if (e.getSource() == button1) {
  60             synchronized (lock) {
  61                 System.err.println("failed: focus on the wrong button");
  62                 System.exit(2);
  63             }
  64         }
  65     }
  66 
  67     public void focusLost(FocusEvent e) {
  68     }
  69 
  70     public void start() {
  71         Thread thread = new Thread(new Runnable() {
  72             public void run() {
  73                 try {
  74                     Thread.sleep(10000);
  75                     synchronized (lock) {
  76                         System.err.println("passed");
  77                         System.exit(0);
  78                     }
  79                 } catch(InterruptedException e) {
  80                 }
  81             }
  82         });
  83         thread.start();
  84     }
  85 }