< prev index next >

test/jdk/java/awt/Focus/AppletInitialFocusTest/AppletInitialFocusTest1.java

Print this page


   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 }
   1 /*
   2  * Copyright (c) 2008, 2018, 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.awt.*;
  25 import java.awt.event.*;
  26 
  27 /*
  28   @test
  29   @key headful
  30   @bug 4411534 4517274
  31   @summary ensures that user's requestFocus() during applet initialization
  32            is not ignored
  33   @library ../../regtesthelpers
  34   @build   Util
  35   @run main AppletInitialFocusTest1
  36  */
  37 public class AppletInitialFocusTest1 extends Frame implements FocusListener {
  38 
  39     Button button1 = new Button("Button1");
  40     Button button2 = new Button("Button2");
  41 
  42     Object lock = new Object();
  43 
  44     public static void main(final String[] args) throws Exception {
  45         AppletInitialFocusTest1 app = new AppletInitialFocusTest1();
  46         app.init();
  47         Thread.sleep(10000);
  48     }
  49 
  50     public void init() {
  51         setSize(200, 200);
  52         setLocationRelativeTo(null);
  53         setLayout(new FlowLayout());
  54 
  55         Component parent = this;
  56         while (parent != null && !(parent instanceof Window)) {
  57             parent = parent.getParent();
  58         }
  59         /*
  60          * This applet is designed to be run only with appletviewer,
  61          * so there always should be a toplevel frame.
  62          */
  63         if (parent == null) {
  64             synchronized (lock) {
  65                 System.err.println("appletviewer not running");
  66                 System.exit(3);
  67             }
  68         }
  69         button1.addFocusListener(this);
  70         button2.addFocusListener(this);
  71         add(button1);
  72         add(button2);
  73         setVisible(true);
  74         button2.requestFocus();
  75     }
  76 
  77     public void focusGained(FocusEvent e) {
  78         if (e.getSource() == button1) {
  79             synchronized (lock) {
  80                 throw new RuntimeException("failed: focus on the wrong button");

  81             }
  82         }
  83     }
  84 
  85     public void focusLost(FocusEvent e) {
  86     }
















  87 }
< prev index next >