Path: blob/master/test/jdk/java/awt/Focus/AppletInitialFocusTest/AppletInitialFocusTest1.java
41152 views
/*1* Copyright (c) 2008, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.Button;24import java.awt.FlowLayout;25import java.awt.Frame;26import java.awt.event.FocusEvent;27import java.awt.event.FocusListener;2829/**30* @test31* @key headful32* @bug 4411534 451727433* @summary ensures that user's requestFocus() during applet initialization34* is not ignored35*/36public class AppletInitialFocusTest1 extends Frame implements FocusListener {3738Button button1 = new Button("Button1");39Button button2 = new Button("Button2");40private static volatile Object focused;4142public static void main(final String[] args) throws Exception {43AppletInitialFocusTest1 app = new AppletInitialFocusTest1();44try {45app.setSize(200, 200);46app.setLocationRelativeTo(null);47app.setLayout(new FlowLayout());4849app.button1.addFocusListener(app);50app.button2.addFocusListener(app);51app.add(app.button1);52app.add(app.button2);53app.setVisible(true);54app.button2.requestFocus();55// wait for the very very last focus event56Thread.sleep(10000);57if (app.button2 != focused) {58throw new RuntimeException("Wrong focus owner: " + focused);59}60} finally {61app.dispose();62}63}6465public void focusGained(FocusEvent e) {66focused = e.getSource();67System.out.println("focused = " + focused);68}6970public void focusLost(FocusEvent e) {71}72}737475