0

I have a problem getting the Column index working in JavaFX's GridPane.

I am setting up the grid in two phases. The first method distributes a few buttons in a 6x6 grid:

Button actualButton = new Button(ge.toString());
actualButton.getStyleClass().add("button");
rootNode.add(actualButton, gf.getX(), gf.getY());

where gf.getX() and gf.getY() are randomized numbers between 1 and 6 (making sure that no two buttons are put in the same cell.

This part works as expected. The problem is with the second part, when I want to put a Label below the 6x6 grid (ideally with a colspan of 6). No matter how I try, the Label will open a brand new column and push everything one column to the right. It is interesting, because I can set the row index, but not the column index. It also won't react to colspan.

This is the code I'm using, although I have tried various ways:

emptyFieldCounter = new Label("Number of empty fields: " + emptyFields.size());
rootNode.add(emptyFieldCounter,1,7,6,1);

I have tried to change the first int (that is supposedly the column index) to every possible value I could think of - nothing changes. If I change the second int, it will move to a different row, but remain in the same column.

here's a screenshot with 7 as the second int: enter image description here

and one after changing the 2nd int after changing the col index and row index values to:

rootNode.add(emptyFieldCounter,3,3,6,1);

enter image description here

any hint would be very much appreciated. cheers, Zsolt

Tirth Patel
  • 5,443
  • 3
  • 27
  • 39
Zsolt Balla
  • 533
  • 2
  • 7
  • 16
  • Btw, I know the value of empty slots is not correct, it's not a problem atm :-) – Zsolt Balla Apr 03 '16 at 14:06
  • Can you create a [MCVE]? Your first version (`rootNode.add(emptyFieldCounter,1,7,6,1);`) works just fine in my quick test. – James_D Apr 03 '16 at 14:26
  • 1
    I guess... check that `gf.getX()` is giving you the values you think it is – James_D Apr 03 '16 at 14:43
  • Wow, when going for the minimal version, I drew up a quick 6x6 grid with this simple code: `private void drawUp6by6Grid(){ for (int i=0;i<6;i++) { Button button = new Button("i"); rootNode.add(button,i,i); } }` and following this method, the `rootNode.add(emptyFieldCounter,1,7,6,1);` worked exactly as expected. So I may have to problem completely elsewhere, not where I was looking for it... Will let you know once I find it, thanks for the hint, anyway :-) – Zsolt Balla Apr 03 '16 at 14:46
  • MCVEs are always useful for identifying where the problem is... – James_D Apr 03 '16 at 14:49
  • Thanks, @James_D - exactly as you suggested: gf.getX() wasn't giving a random value between 1-6, but a random value between A-F. but as char translates to int perfectly, the GridPane didn't complain Thanks again for your quick help! – Zsolt Balla Apr 03 '16 at 14:55

0 Answers0