AlexanderJ93 AlexanderJ93 - 1 year ago 325
Java Question

JavaFX GridPane col/row span vs index

I'm learning JavaFX through the Oracle Tutorial and I'm confused about this particular topic. I seem to be the only one confused as my searching hasn't led to any helpful results. I'm creating a basic GridPane with 2 columns (0,1) and 3 rows (0,1,2). In the first row I have the title, and the other two rows have a label on the left and a text box on the right -- it's a login screen

LOGIN

username | [_______]

password | [_______]

Here's the code for populating the grid:

Text scenetitle = new Text("Login");
scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
grid.add(scenetitle,0,0,2,1); // Row span here is set to 1

Label userName = new Label("User Name");
grid.add(userName,0,1);

TextField userTField = new TextField();
grid.add(userTField,1,1);

Label passName = new Label("Password");
grid.add(passName,0,2); // Here I'm referencing a row index of 2

TextField passTField = new TextField();
grid.add(passTField,1,2)


The code works perfectly, as it's basically just a copy-paste from the tutorial. However, my confusion comes from the fact that I have a row span of 1, but I'm referencing a row index of 2. I tried switching the row and column span but that messed it up, so I'm sure this is correct. What am I missing?

Answer Source

It is different.First of all it is usefull to:

      setGridLinesVisible(true);

so you can see what is going on with the grid layout(the space between columns and rows the width and height of elements etc). About add method from documentation:

add(Node child,int columnIndex,int rowIndex,int colspan,int rowspan)

In the first line:

       grid.add(scenetitle,0,0,2,1);

you add the element scenetitle and it will be positioned on column 0 and row 0 and it has the possibility to span 2 columns and 1 row

In the finish line:

      grid.add(passName,0,2); 

you add the element passName and it will be positioned on column 0 and row 2 ,you have not given values about how it can span.

A special situation

In GridPane if for example you have an item on column 0 and row 0 with row span 4,and in the second line an item with not default span given it will span the maximum of the spans (in this example 4).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download