a e s t h e t i c a e s t h e t i c - 10 days ago 5
Java Question

Java FX change Halignment of Component

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


than I add them to a GridPane

checkBoxes
.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

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

alignment

Code

@Override
public void start(Stage primaryStage) throws Exception {
    GridPane grid = new GridPane();
    CheckBox box1 = new CheckBox("horse");
    box1.setMinWidth(200);
    box1.setAlignment(Pos.CENTER_RIGHT);
    grid.getChildren().add(box1);
    CheckBox box2 = new CheckBox("banana");
    grid.getChildren().add(box2);
    GridPane.setRowIndex(box2, 1);
    GridPane.setHalignment(box2, HPos.RIGHT);   // <=== this is probably what you want
    primaryStage.setScene(new Scene(grid));
    primaryStage.show();
}

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.