import java.awt.*;
import java.awt.event.*;

import javafx.application.Application;
import javafx.embed.swing.SwingNode;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

import javax.swing.*;

public class Test extends Application {

    private JButton b;
    private Point   location;
    private Robot   robot;

    @Override
    public void start(final Stage stage) throws Exception {

        Thread.setDefaultUncaughtExceptionHandler((Thread t, Throwable e) -> {
            System.err.println("Uncaught exception on thread : " + t);
            System.exit(1);
        });

        robot = new Robot();

        SwingNode swingNode = new SwingNode();
        init(swingNode);
        StackPane pane = new StackPane();
        pane.getChildren().add(swingNode);
        Scene scene = new Scene(pane, 200, 200);
        stage.setScene(scene);
        stage.setX(150);
        stage.setY(150);
        robot.mouseMove(0, 0);
        stage.setOnShown(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent t) {
                new Thread(() -> {
                    try {
                        delay(2000);

                        SwingUtilities.invokeAndWait(() -> {
                            location = b.getLocationOnScreen();
                            System.out.println("location: " + location.x + ", " + location.y);
                        });

                        SwingUtilities.invokeAndWait(() -> {
                            robot.mouseMove(location.x + 5, location.y + 5);
                            delay(1000);
                            click();
                        });

                        SwingUtilities.invokeAndWait(() -> {
                            robot.mouseMove(0, 0);
                            delay(1000);
                        });

                        SwingUtilities.invokeAndWait(() -> {
                            robot.mouseMove(location.x + 5, location.y + 5);
                            delay(1000);
                            click();
                        });
                        
                    } catch (Exception e) {
                        e.printStackTrace();
                        System.exit(1);
                    }
                }).start();
            }
        });
        stage.show();
    }
    
    private void click() {
        robot.mousePress(InputEvent.BUTTON1_MASK);
        delay(100);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        delay(100);
    }

    private void init(final SwingNode node) throws InterruptedException {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JPanel panel = new JPanel();
                panel.setLayout(null);
                b = new JButton("JButton");
                b.setLocation(25, 25);
                b.setSize(100, 100);
                
                b.addActionListener((ActionEvent e) -> { System.out.println("test!"); });

                panel.add(b);
                node.setContent(panel);
            }
        });
    }

    public void delay(int delay) { try { Thread.sleep(delay); } catch (Exception e) {} }
    public static void main(String[] args) { launch(args); }
}
