0

I'm trying to center a button in JavaFX using Gridpane. I was told to use colspan and rowspan, but I can't figure out how those work.

    pane.add(label1, 0, 1);
    pane.add(text1, 1, 1);
    pane.add(radioButton1, 0, 2);
    pane.add(radioButton2, 1, 2);
    pane.add(label2, 0, 3);
    pane.add(text2, 1, 3);
    pane.add(button, 0, 4); //What I'm trying to span.

This is what I have, and This is where I want the button to be.

Kennedy
  • 1
  • 1
  • 2
    Does this answer your question? [JavaFx GridPane - how to center elements](https://stackoverflow.com/questions/12816075/javafx-gridpane-how-to-center-elements) – Dan Apr 17 '22 at 16:04
  • Also consider the approach examined [here](https://stackoverflow.com/q/31909941/230513), which obviates the need for a calculate button altogether. – trashgod Apr 17 '22 at 17:01
  • 1
    You need to make the button span two columns, and then center it, so it is centered across both columns. Just read the API docs for the method calls. – James_D Apr 17 '22 at 17:37

1 Answers1

1

I originally thought you were trying to center each line. After seeing James_D's comment, it seems that you are only having an issue with the calculate Button.

For that try:

pane.add(button, 0, 4, 2, 1); //add(Node child, int columnIndex, int rowIndex, int colspan, int rowspan)
GridPane.setHalignment(button, HPos.CENTER);
SedJ601
  • 12,173
  • 3
  • 41
  • 59