1 /*
   2  * Copyright (c) 2016, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package hellotest;
  27 
  28 import javafx.application.Application;
  29 import javafx.event.ActionEvent;
  30 import javafx.scene.Group;
  31 import javafx.scene.Scene;
  32 import javafx.scene.control.Button;
  33 import javafx.scene.input.KeyCode;
  34 import javafx.scene.input.KeyEvent;
  35 import javafx.scene.robot.Robot;
  36 import javafx.stage.Stage;
  37 
  38 public class RemoveFocusedControl extends Application {
  39 
  40     public static void main(String[] args) {
  41         Application.launch(args);
  42     }
  43 
  44     @Override
  45     public void start(Stage stage) {
  46 
  47         final Group root = new Group();
  48         stage.setTitle("Remove Focused Control Test");
  49         Scene scene = new Scene(root, 600, 450);
  50 
  51         scene.setOnKeyTyped((javafx.scene.input.KeyEvent e) -> {
  52             System.out.println("Event: " + e);
  53         });
  54 
  55         Button button1 = new Button();
  56         button1.setText("Click HERE for removing this button and then sending "
  57                 + "four key events");
  58         button1.setLayoutX(25);
  59         button1.setLayoutY(40);
  60 
  61         final Robot robot = new Robot();
  62 
  63         button1.setOnAction((ActionEvent e) -> {
  64             root.getChildren().remove(button1);
  65             robot.keyPress(KeyCode.T);
  66             robot.keyRelease(KeyCode.T);
  67             robot.keyPress(KeyCode.E);
  68             robot.keyRelease(KeyCode.E);
  69             robot.keyPress(KeyCode.S);
  70             robot.keyRelease(KeyCode.S);
  71             robot.keyPress(KeyCode.T);
  72             robot.keyRelease(KeyCode.T);
  73         });
  74 
  75         root.getChildren().add(button1);
  76 
  77         stage.setScene(scene);
  78         stage.show();
  79         System.out.println("Please press on 'Click HERE...' button in order to remove\n"
  80                 + " the button and generate automatically four keyEvents.\n"
  81                 + "Expected results: Button is disappearing, exactly FOUR KeyEvents\n"
  82                 + " are being accepted and printed to the console.");
  83     }
  84 }