1 /*
   2   test %W% %E%
   3   @bug 4411534 4517274
   4   @summary ensures that user's requestFocus() during applet initialization
   5            is not ignored.
   6   @author  prs@sparc.spb.su area=appletviewer
   7   @run shell AppletInitialFocusTest1.sh
   8 */
   9 
  10 import java.applet.Applet;
  11 import java.awt.*;
  12 import java.awt.event.*;
  13 
  14 public class AppletInitialFocusTest1 extends Applet implements FocusListener {
  15 
  16     Button button1 = new Button("Button1");
  17     Button button2 = new Button("Button2");
  18 
  19     Object lock = new Object();
  20 
  21     public void init() {
  22 
  23         Component parent = this;
  24         while (parent != null && !(parent instanceof Window)) {
  25             parent = parent.getParent();
  26         }
  27         /*
  28          * This applet is designed to be run only with appletviewer,
  29          * so there always should be a toplevel frame.
  30          */
  31         if (parent == null) {
  32             synchronized (lock) {
  33                 System.err.println("appletviewer not running");
  34                 System.exit(3);
  35             }
  36         }
  37         button1.addFocusListener(this);
  38         button2.addFocusListener(this);
  39         add(button1);
  40         add(button2);
  41         button2.requestFocus();
  42     }
  43 
  44     public void focusGained(FocusEvent e) {
  45         if (e.getSource() == button1) {
  46             synchronized (lock) {
  47                 System.err.println("failed: focus on the wrong button");
  48                 System.exit(2);
  49             }
  50         }
  51     }
  52 
  53     public void focusLost(FocusEvent e) {
  54     }
  55 
  56     public void start() {
  57         Thread thread = new Thread(new Runnable() {
  58             public void run() {
  59                 try {
  60                     Thread.sleep(10000);
  61                     synchronized (lock) {
  62                         System.err.println("passed");
  63                         System.exit(0);
  64                     }
  65                 } catch(InterruptedException e) {
  66                 }
  67             }
  68         });
  69         thread.start();
  70     }
  71 }