package inputboxes;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class InputBoxesDemo extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
    	// Set ChoiseBox - drop down menu
    	ChoiceBox<String> choiceBox = new ChoiceBox<>();
        choiceBox.getItems().add("Option 1");
        choiceBox.getItems().add("Option 2");
        choiceBox.getItems().addAll("Option 3", "Option 4");
        choiceBox.setValue("Option 2");
//        choiceBox.setOnAction( e -> System.out.println("User selected " + choiceBox.getValue()));
        choiceBox.getSelectionModel().selectedItemProperty().addListener((v, oldValue, newValue) -> {
        	System.out.println("Previous selection " + oldValue + ", new selection " + newValue);
        });
        
        //Button
        Button cbButton = new Button("Ok");
        cbButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent arg0) {
				String message = "Selected choise: " + choiceBox.getValue();
		        System.out.println(message);
			}
        });

        //Layout
        VBox cbLayout = new VBox(10);
        cbLayout.setPadding(new Insets(20, 20, 20, 20));
        cbLayout.getChildren().addAll(choiceBox, cbButton);
        
        //----------------------------------------------------
        // Set ComboBox 
        ComboBox<String> comboBox = new ComboBox<>();
        comboBox.getItems().add("Option 1");
        comboBox.getItems().add("Option 2");
        comboBox.getItems().addAll("Option 3", "Option 4");
        comboBox.setPromptText("What is ypur choise?");
        comboBox.setEditable(true);
        comboBox.setOnAction( e -> System.out.println("User selected " + comboBox.getValue()));

        //Button
        Button cmbButton = new Button("Ok");
        cmbButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent arg0) {
				String message = "Selected choise: " + comboBox.getValue();
		        System.out.println(message);
			}
        });

        //Layout
        VBox cmbLayout = new VBox(10);
        cmbLayout.setPadding(new Insets(20, 20, 20, 20));
        cmbLayout.getChildren().addAll(comboBox, cmbButton);


        HBox layout = new HBox(20);
        layout.getChildren().add(cbLayout);
        layout.getChildren().add(cmbLayout);
        primaryStage.setTitle("Input Boxes Demo");
        primaryStage.setScene(new Scene(layout));
        primaryStage.show();
    }
}

