Path: blob/master/test/jdk/javax/swing/InputVerifier/VerifyTarget/VerifyTargetTest.java
41152 views
/*1* Copyright (c) 2016, 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*/2223/*24@test25@key headful26@bug 815443127@summary Allow source and target based validation for the focus transfer28between two JComponents.29@run main VerifyTargetTest30*/3132import javax.swing.*;33import java.awt.*;34import java.awt.event.FocusEvent;35import java.awt.event.FocusListener;3637public class VerifyTargetTest extends InputVerifier implements FocusListener {38static boolean success;39private static JFrame frame;40private static JTextField field2;4142public static void main(String[] args) throws Exception {43SwingUtilities.invokeAndWait(() -> setup());44try {45Robot robot = new Robot();46robot.waitForIdle();47robot.delay(200);48KeyboardFocusManager.getCurrentKeyboardFocusManager()49.focusNextComponent();50robot.waitForIdle();51robot.delay(200);52if (!success) {53throw new RuntimeException("Failed");54} else {55System.out.println("ok");56}57} finally {58SwingUtilities.invokeLater(() -> frame.dispose());59}60}6162static void setup() {63frame = new JFrame();64JTextField field1 = new JTextField("Input 1");65VerifyTargetTest test = new VerifyTargetTest();66field1.setInputVerifier(test);67field1.addFocusListener(test);68frame.getContentPane().add(field1, BorderLayout.NORTH);69field2 = new JTextField("Input 2");70frame.getContentPane().add(field2, BorderLayout.SOUTH);71frame.pack();72frame.setVisible(true);73}7475@Override76public boolean verify(JComponent input) {77return true;78}7980@Override81public boolean verifyTarget(JComponent input) {82success = input == field2;83return false;84}8586@Override87public void focusGained(FocusEvent e) {}8889@Override90public void focusLost(FocusEvent e) {91success = false;92}93}949596