import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 * The simplest HBox: three labels in a row.
 * Resize the window and watch what stays put and what moves.
 *
 *   setPadding    space INSIDE the HBox, between its edge and its children
 *   setSpacing    space BETWEEN neighboring children
 *   setMargin     space OUTSIDE one child (set through HBox, not the child)
 *   setAlignment  where the row of children sits inside the HBox
 *   setHgrow      which child takes the spare width
 *
 * Edit a value, run again, resize the window.
 */

public class SimpleHBoxDemo extends Application {

	// A colored label with a fixed preferred size so you can see its edges.
	private Label label ( String text, String color ) {
		Label label = new Label(text);
		label.setPrefSize(80,50);
		label.setAlignment(Pos.CENTER); // the text inside the label
		label.setStyle("-fx-background-color: " + color
		    + "; -fx-border-color: #333;");
		return label;
	}

	@Override
	public void start ( Stage stage ) {
		Label one = label("1","#cfe8ff");
		Label two = label("2","#c8f0c8");
		Label three = label("3","#ffe0b3");

		HBox row = new HBox(one,two,three);
		row.setStyle("-fx-background-color: #eeeeee;");

		// ---- setPadding: space inside the HBox's edge ----
		row.setPadding(new Insets(15)); // top, right, bottom, left all 15
		// try: row.setPadding(new Insets(40, 10, 10, 10)); // top, right, bottom,
		// left

		// ---- setSpacing: space between children ----
		row.setSpacing(10);
		// try: row.setSpacing(30);

		// ---- setMargin: space around ONE child (note: it is an HBox method) ----
		HBox.setMargin(two,new Insets(0,0,0,20)); // top, right, bottom, left
		// try: HBox.setMargin(two, new Insets(20));

		// ---- setAlignment: where the children sit inside the HBox ----
		row.setAlignment(Pos.CENTER_LEFT); // the default is TOP_LEFT
		// try: row.setAlignment(Pos.BOTTOM_RIGHT);
		// try: row.setAlignment(Pos.CENTER);

		// ---- grow: give the spare width to one child ----
		// try (uncomment both lines; label 3 stretches and alignment no longer
		// moves it sideways):
		// three.setMaxWidth(Double.MAX_VALUE); // allowed to stretch
		// HBox.setHgrow(three, Priority.ALWAYS); // gets the spare width

		stage.setScene(new Scene(row,480,160));
		stage.setTitle("HBox");
		stage.show();
	}

	public static void main ( String[] args ) {
		launch(args);
	}
}
