1 /*
  2  * Copyright (c) 2003, 2023, 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 /*
 25  * @test
 26  * @bug 4822331
 27  * @summary setLaberFor does not transfer focus to the JSpinner editor
 28  * @library /test/lib
 29  * @key headful
 30  * @run main bug4822331
 31  */
 32 
 33 import java.awt.event.FocusAdapter;
 34 import java.awt.event.FocusEvent;
 35 import java.awt.event.KeyEvent;
 36 import java.awt.FlowLayout;
 37 import java.awt.Robot;
 38 import javax.swing.JButton;
 39 import javax.swing.JFormattedTextField;
 40 import javax.swing.JFrame;
 41 import javax.swing.JLabel;
 42 import javax.swing.JSpinner;
 43 import javax.swing.SwingUtilities;
 44 import jdk.test.lib.Platform;
 45 
 46 public class bug4822331 {
 47 
 48     static JFrame fr;
 49     static JButton button;
 50     static JSpinner spinner;
 51     static volatile boolean tfFocused = false;
 52     static volatile boolean passed = false;
 53 
 54     public static void main(String []args) throws Exception {
 55         bug4822331 test = new bug4822331();
 56         test.init();
 57     }
 58 
 59     public void init() throws Exception {
 60         try {
 61             SwingUtilities.invokeAndWait(() -> {
 62                 fr = new JFrame("Test");
 63                 fr.getContentPane().setLayout(new FlowLayout());
 64 
 65                 button = new JButton("Button");
 66                 fr.getContentPane().add(button);
 67 
 68                 spinner = new JSpinner();
 69                 JLabel spinnerLabel = new JLabel("spinner");
 70                 spinnerLabel.setDisplayedMnemonic(KeyEvent.VK_S);
 71                 spinnerLabel.setLabelFor(spinner);
 72                 fr.getContentPane().add(spinnerLabel);
 73                 fr.getContentPane().add(spinner);
 74 
 75                 JSpinner.DefaultEditor editor =
 76                         (JSpinner.DefaultEditor) spinner.getEditor();
 77                 JFormattedTextField ftf = editor.getTextField();
 78                 ftf.addFocusListener(new FocusAdapter() {
 79                     public void focusGained(FocusEvent e) {
 80                         passed = true;
 81                     }
 82                 });
 83                 fr.pack();
 84                 fr.setVisible(true);
 85             });
 86             start();
 87             if ( !passed ) {
 88                 throw new RuntimeException("The activation of spinner's " +
 89                         "mnemonic didn't focus the editor component.");
 90             }
 91         } finally {
 92             SwingUtilities.invokeAndWait(() -> {
 93                 if (fr != null) {
 94                     fr.dispose();
 95                 }
 96             });
 97         }
 98     }
 99 
100     public void start() throws Exception {
101         Robot robot = new Robot();
102         robot.setAutoDelay(50);
103         robot.delay(1000);
104         robot.waitForIdle();
105         button.requestFocus();
106         if (Platform.isOSX()) {
107             robot.keyPress(KeyEvent.VK_CONTROL);
108             robot.keyPress(KeyEvent.VK_ALT);
109             robot.keyPress(KeyEvent.VK_S);
110             robot.keyRelease(KeyEvent.VK_S);
111             robot.keyRelease(KeyEvent.VK_ALT);
112             robot.keyRelease(KeyEvent.VK_CONTROL);
113         } else {
114             robot.keyPress(KeyEvent.VK_ALT);
115             robot.keyPress(KeyEvent.VK_S);
116             robot.keyRelease(KeyEvent.VK_S);
117             robot.keyRelease(KeyEvent.VK_ALT);
118         }
119     }
120 }