import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import javafx.stage.Stage;

/**
 * The simplest GridPane: labels in a 3 x 3 grid with the end of the middle row
 * and the beginning of the last row each span two columns.
 * The grid lines are switched on so you can see the cells.
 *
 *   setHgap / setVgap   space BETWEEN columns / rows
 *   setPadding          space INSIDE the grid, between its edge and the cells
 *   setMargin           space around ONE node, inside its cell (set through GridPane)
 *   setHalignment / setValignment   where a smaller node sits inside its cell
 *   ColumnConstraints / RowConstraints   sizes and grow settings for whole columns or rows
 *
 * Edit a value, run again, resize the window.
 */

public class SimpleGridPaneDemo 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(90,40);
		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 a = label("col 0, row 0","#cfe8ff");
		Label b = label("col 1, row 0","#cfe8ff");
		Label c = label("col 2, row 0","#cfe8ff");
		Label d = label("col 0, row 1","#cfe8ff");
		Label e = label("cols 1-2, row 1 (spans 2 columns)","#ffe0b3");
		Label f = label("cols 0-1, row 2 (spans 2)","#c8f0c8");
		Label g = label("col 2, row 2","#cfe8ff");

		GridPane grid = new GridPane();
		grid.add(a,0,0); // add(node, column, row)
		grid.add(b,1,0);
		grid.add(c,2,0);
		grid.add(d,0,1);
		grid.add(e,1,1,2,1); // add(node, column, row, columnSpan, rowSpan)
		grid.add(f,0,2,2,1); // starts in column 0 and spans columns 0 and 1
		grid.add(g,2,2);
		e.setMaxWidth(Double.MAX_VALUE); // lets a spanning label fill all the
		                                 // columns it spans
		f.setMaxWidth(Double.MAX_VALUE);
		grid.setStyle("-fx-background-color: #eeeeee;");
		grid.setGridLinesVisible(true); // draws the cell edges (for debugging)

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

		// ---- setHgap / setVgap: space between columns / rows ----
		grid.setHgap(10);
		grid.setVgap(10);
		// try: grid.setHgap(30);

		// ---- column and row constraints: sizes of whole columns and rows ----
		// try: comment out this whole section
		ColumnConstraints col0 = new ColumnConstraints();
		col0.setPrefWidth(120); // wider than the label in it
		ColumnConstraints col1 = new ColumnConstraints();
		col1.setPrefWidth(120);
		ColumnConstraints col2 = new ColumnConstraints();
		col2.setPrefWidth(120);
		grid.getColumnConstraints().addAll(col0,col1,col2); // the first one is
		                                                    // column 0
		RowConstraints row0 = new RowConstraints();
		row0.setPrefHeight(70); // taller than the label in it
		grid.getRowConstraints().add(row0); // the first one is row 0
		// try: col2.setHgrow(Priority.ALWAYS); // column 2 takes spare width when
		// you resize
		// try: col0.setPercentWidth(25); col1.setPercentWidth(25);
		// col2.setPercentWidth(50);

		// ---- setMargin: space around ONE node, inside its cell (note: a GridPane
		// method) ----
		GridPane.setMargin(c,new Insets(0,0,0,20)); // top, right, bottom, left
		// try: GridPane.setMargin(a, new Insets(10));

		// ---- setHalignment / setValignment: where a node sits in a bigger cell
		// ----
		GridPane.setHalignment(a,HPos.CENTER); // the default is LEFT
		GridPane.setValignment(a,VPos.TOP); // the default is CENTER
		// try: GridPane.setHalignment(c, HPos.RIGHT);
		// try: GridPane.setValignment(d, VPos.BOTTOM);

		stage.setScene(new Scene(grid,480,280));
		stage.setTitle("GridPane");
		stage.show();
	}

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