I have a class that describes the right panel of my GUI application with buttons :

public class RightMenu extends GridPane { public RightMenu() { Button startButton = new Button("Старт"); Button stopButton = new Button("Стоп"); Button clearButton = new Button("Очистить график"); this.add(startButton, 0, 0); this.add(stopButton, 1, 0); this.add(clearButton, 0, 1); this.setPadding(new Insets(2, 2, 2, 2)); } 

}

It looks like this: enter image description here

As you can see the buttons are very crooked. How can I make the "Clear graph" button under both the "Start" and "Stop" buttons, that is, occupy two columns? And how do you correctly align the buttons?

  • What layout are you using? - Komdosh
  • inherited from GridPane - faoxis
  • Oh, did not notice, it is necessary to set the cell merge this.setColumnSpan (Node child, Integer value) - Komdosh
  • And what to indicate as a child and an integer value? - faoxis

2 answers 2

In order to do the way you want, you need to call the method

 GridPane.setColumnSpan(clearButton,2); 

This method will stretch the button into 2 columns. Just to be in the center (horizontal), you need to do so

 GridPane.setHalignment(clearButton, HPos.CENTER); 

For vertical alignment, you need to use setValigment

  • Everything worked out, but the lower clearButton button took up much more space than the upper startButton and stopButton, which caused extra space in the upper right corner. How to get rid of it? - faoxis
  • Make a GridPane.setHaligment (stopButton, HPos.RIGHT) - Andrew Bystrov
  • GridPane does not know the setHaligment method ... - faoxis
  • GridPane.setHalignment - Andrew Bystrov

My version is a bit shaggy, but as an option, it also fits. The bottom line is that instead of the grid, charge focus with Hbox and VBox. We take and create Vbox and Hbox

 VBox vbox = new VBox(); HBox hbox = new HBox(); 

Those buttons that should be in a row are charged in the hbox

 hbox.getChildren().addAll(startButton, stopButton); 

So we got a row of two buttons, and now we are building a column of this row and another button

 vbox.getChildren().addAll(hbox, clearButton); 

PS I still hope that there is an adequate way to join the cells in the table. But at that time I did not bother searching and just did so. (Fortunately, I also had a small socket)

  • This is not an option, because the sizes will go away and it turns out that the lower button is much larger than the upper two leaves ahead (tried). You can, of course, substitute spaces, but these are crutches. - faoxis
  • Leaves ahead? And if you correct the size of the buttons or spacing from the hbox set? - Riĥard Brugekĥaim
  • Can be more ? - faoxis
  • spacing for VBox and HBox is the distance between adjacent nodes. - Andrew Bystrov
  • 2faoxis And just do it, I said and said: Use FXML. There you and MVC, and a visual editor. - Riĥard Brugekĥaim