a e s t h e t i c a e s t h e t i c - 9 months ago 49
Java Question

Java FX change Halignment of Component

public CheckBox createBox(){
CheckBox box = new CheckBox();
box.setOnMouseClicked(x -> handlePlaying(box));
return box;

than I add them to a GridPane

.forEach(x -> gridPane.add(x, checkBoxes.indexOf(x) + 1, 2));

Now I want to change the Halignment, but I can't find the method.

PS: I Want to Change the Halignement not the general Alignement;

Answer Source

I believe this example illustrates the differences between Labelled.setAlignment() and GridPane.setHAlignment().



public void start(Stage primaryStage) throws Exception {
    GridPane grid = new GridPane();
    CheckBox box1 = new CheckBox("horse");
    CheckBox box2 = new CheckBox("banana");
    GridPane.setRowIndex(box2, 1);
    GridPane.setHalignment(box2, HPos.RIGHT);   // <=== this is probably what you want
    primaryStage.setScene(new Scene(grid));

I'd recommend FXML over programmatic layout, but same principles apply. Find the HAlignment property in Layout tab in SceneBuilder...

Labelled.setAlignment() does explain this already in its JavaDoc, although I can see why this might be confusing.

Specifies how the text and graphic within the Labeled should be aligned when there is empty space within the Labeled.