Search code examples
javacssalignmentjavafx-8

Layout of elements inside of HBox for JavaFX


I want to loop through each object of a list. For each entry I want to create a GUI object that looks like this:

  • A checkbox on the left
  • An image in the center
  • (later) A label on the left

My problem is, that each label has a different length and it looks rather strange if not all pictures are on the same line (as seen vertically). Is there a possibility by either java or css to align the ImageVew in center of the HBox?
imageView.setLayoutX(filterBox.getWidth()/2); didn't do the trick unfortunatly. And no -fx-align: right; or -fx-float: right; seems to be existing.
I included what I have so far.

VBox filtersBox = new VBox();
HBox filterBox;
for(Filter filter : filters.getFilters()){
     if(!filter.isComplex()){
          filterBox = new HBox();
          filterBox.getStyleClass().add("filter");
          ImageView imageView = new ImageView();
          [image view stuff]
          final CheckBox cbox = new CheckBox(filter.getName().toString());
          filterBox.getChildren().addAll(cbox, imageView);
          filtersBox.getChildren().addAll(filterBox);
     }
}

Solution

  • As far as I'm aware, this is impossible.

    I see two ways you can achieve this layout, though:

    1. Have all the checkboxes have the same (constant) preferred width. This way your image views should line up.
    2. Use a GridPane, and add rows instead of HBoxes