AppletInitialFocusTest1.java revision 8729:0242fce0f717
1321936Shselasky/*
2321936Shselasky * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
3321936Shselasky * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4321936Shselasky *
5321936Shselasky * This code is free software; you can redistribute it and/or modify it
6321936Shselasky * under the terms of the GNU General Public License version 2 only, as
7321936Shselasky * published by the Free Software Foundation.
8321936Shselasky *
9321936Shselasky * This code is distributed in the hope that it will be useful, but WITHOUT
10321936Shselasky * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11321936Shselasky * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12321936Shselasky * version 2 for more details (a copy is included in the LICENSE file that
13321936Shselasky * accompanied this code).
14321936Shselasky *
15321936Shselasky * You should have received a copy of the GNU General Public License version
16321936Shselasky * 2 along with this work; if not, write to the Free Software Foundation,
17321936Shselasky * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18321936Shselasky *
19321936Shselasky * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20321936Shselasky * or visit www.oracle.com if you need additional information or have any
21321936Shselasky * questions.
22321936Shselasky */
23321936Shselasky
24321936Shselaskyimport java.applet.Applet;
25321936Shselaskyimport java.awt.*;
26321936Shselaskyimport java.awt.event.*;
27321936Shselasky
28321936Shselaskypublic class AppletInitialFocusTest1 extends Applet implements FocusListener {
29321936Shselasky
30321936Shselasky    Button button1 = new Button("Button1");
31321936Shselasky    Button button2 = new Button("Button2");
32321936Shselasky
33321936Shselasky    Object lock = new Object();
34321936Shselasky
35321936Shselasky    public void init() {
36321936Shselasky
37321936Shselasky        Component parent = this;
38321936Shselasky        while (parent != null && !(parent instanceof Window)) {
39321936Shselasky            parent = parent.getParent();
40321936Shselasky        }
41321936Shselasky        /*
42321936Shselasky         * This applet is designed to be run only with appletviewer,
43321936Shselasky         * so there always should be a toplevel frame.
44321936Shselasky         */
45321936Shselasky        if (parent == null) {
46321936Shselasky            synchronized (lock) {
47321936Shselasky                System.err.println("appletviewer not running");
48321936Shselasky                System.exit(3);
49321936Shselasky            }
50321936Shselasky        }
51321936Shselasky        button1.addFocusListener(this);
52321936Shselasky        button2.addFocusListener(this);
53321936Shselasky        add(button1);
54321936Shselasky        add(button2);
55321936Shselasky        button2.requestFocus();
56321936Shselasky    }
57321936Shselasky
58321936Shselasky    public void focusGained(FocusEvent e) {
59321936Shselasky        if (e.getSource() == button1) {
60321936Shselasky            synchronized (lock) {
61321936Shselasky                System.err.println("failed: focus on the wrong button");
62321936Shselasky                System.exit(2);
63321936Shselasky            }
64321936Shselasky        }
65321936Shselasky    }
66321936Shselasky
67321936Shselasky    public void focusLost(FocusEvent e) {
68321936Shselasky    }
69321936Shselasky
70321936Shselasky    public void start() {
71321936Shselasky        Thread thread = new Thread(new Runnable() {
72321936Shselasky            public void run() {
73321936Shselasky                try {
74321936Shselasky                    Thread.sleep(10000);
75321936Shselasky                    synchronized (lock) {
76                        System.err.println("passed");
77                        System.exit(0);
78                    }
79                } catch(InterruptedException e) {
80                }
81            }
82        });
83        thread.start();
84    }
85}
86