import javafx.application.Application;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.RadioButton;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {
        Application.launch(args);
    }

    String listenerText = "";
    String logText = "";
        
    @Override
    public void start(Stage primaryStage) throws Exception {
        final RadioButton radioButton = new RadioButton();
        final CheckBox checkBox1 = new CheckBox("I'll get focus - 1");
        final CheckBox checkBox2 = new CheckBox("I'll get focus - 2");
        final IntegerProperty TRANSLATEXVAL = new SimpleIntegerProperty(0);
        final Text text1 = new Text();
        final Text text2 = new Text();
        
        TRANSLATEXVAL.addListener(new InvalidationListener() {
            @Override
            public void invalidated(Observable vm) {
                radioButton.setTranslateX(100 + TRANSLATEXVAL.getValue());
            }
        });
        final IntegerProperty TRANSLATEYVAL = new SimpleIntegerProperty(0);
        TRANSLATEYVAL.addListener(new InvalidationListener() {
            @Override
            public void invalidated(Observable vm) {
                radioButton.setTranslateY(TRANSLATEYVAL.getValue());
            }
        });
        radioButton.setText("Push me, then use up and down to move me");
        radioButton.setPrefSize(200, 200);
        radioButton.setWrapText(true);
        radioButton.setOnKeyTyped(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent e) {
                listenerText = "RadioButton : onKeyTyped";
            }
        });
        radioButton.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent e) {
                listenerText = "toggleButton : onKeyPressed";
                text1.setText("Event fired on " + listenerText);
                if (e.getCode().equals(KeyCode.DOWN)) {
                    TRANSLATEYVAL.setValue(TRANSLATEYVAL.getValue() + 20);
                    logText = "TranslateY " + String.valueOf(TRANSLATEYVAL.getValue());
                    text2.setText("Transformation : " + logText);
                }
                if (e.getCode().equals(KeyCode.UP)) {
                    TRANSLATEYVAL.setValue(TRANSLATEYVAL.getValue() - 20);
                    logText = "TranslateY " + String.valueOf(TRANSLATEYVAL.getValue());
                    text2.setText("Transformation : " + logText);
                }
                if (e.getCode().equals(KeyCode.RIGHT)) {
                    TRANSLATEXVAL.setValue(TRANSLATEXVAL.getValue() + 20);
                    logText = "TranslateX " + String.valueOf(TRANSLATEXVAL.getValue());
                    text2.setText("Transformation : " + logText);
                }
                if (e.getCode().equals(KeyCode.LEFT)) {
                    TRANSLATEXVAL.setValue(TRANSLATEXVAL.getValue() - 20);
                    logText = "TranslateX " + String.valueOf(TRANSLATEXVAL.getValue());
                    text2.setText("Transformation : " + logText);
                }
		e.consume();
            }
        });
        
        VBox layout = new VBox(text1, text2, checkBox1, checkBox2, radioButton);
        primaryStage.setTitle("RadioButtonTest2");
        Scene scene = new Scene(layout);
        primaryStage.setScene(scene);
        primaryStage.setWidth(300);
        primaryStage.setHeight(500);
        primaryStage.show();
    }
}
