import javafx.application.Application;
import javafx.embed.swing.SwingNode;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * Simple SwingNode test program that displays an FX stage with an FX Label
 * and Button, and a Swing JLabel and JButton.
 */
public class SimpleSwingNodeTest extends Application {

    // Set to true to put the Swing JLabel and JButton into a JPanel, using
    // the JPanel as the content of the SwingNode. Set to false to use the
    // Swing JLabel and JButton, respectively, as the content of two separate
    // SwingNodes.
    private static final boolean USE_JPANEL = true;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("JavaFX SwingNode: "
                + "USE_JPANEL:" + USE_JPANEL
                + ", jdk:" + System.getProperty("java.runtime.version")
                + ", javafx:" + System.getProperty("javafx.runtime.version"));

        VBox root = new VBox(5);
        root.setPadding(new Insets(5));

        Label label = new Label("JavaFX: label");

        Button button = new Button("JavaFX: Button");
        button.setOnAction(e -> {
            System.out.print("" + Thread.currentThread() + ": ");
            System.out.println("JavaFX: Button");
        });

        JLabel jLabel = new JLabel("SwingNode: JLabel");
        SwingNode swingLabel = null;
        if (!USE_JPANEL) {
            swingLabel = new SwingNode();
            swingLabel.setContent(jLabel);
        }

        JButton jButton = new JButton("SwingNode: JButton");
        jButton.addActionListener(e -> {
            System.out.print("" + Thread.currentThread() + ": ");
            System.out.println("Swing: JButton in SwingNode");
        });

        SwingNode swingButton = null;
        if (!USE_JPANEL) {
            swingButton = new SwingNode();
            swingButton.setContent(jButton);
        }

        SwingNode swingPanel = null;
        if (USE_JPANEL) {
            JPanel jPanel = new JPanel() {
                {
                    this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
                    this.add(jLabel);
                    this.add(jButton);
                }
            };
            swingPanel = new SwingNode();
            swingPanel.setContent(jPanel);
        }

        if (USE_JPANEL) {
            root.getChildren().addAll(label, button, swingPanel);
        } else {
            root.getChildren().addAll(label, button, swingLabel, swingButton);
        }

        Scene scene = new Scene(root, 800, 600);
        stage.setScene(scene);
        stage.show();
    }

}
