My question is with the layout
I have a little problem with ListView , and I'm not sure if it is due to some knowledge that I lost, or if my approach is wrong. I must admit, I have not yet understood how JavaFX handles the layout in many possible cases.

The above screenshot shows the result that I get twice with the same code, except in the second that the invisible form that I use for the coherent layout becomes visible for debugging .
The various classes involved in CellFactory are expanding by the Group , I have tried with some other Parent without much success so far.
How to play
Instead of sharing my StarShape , StarRow and some other misc classes (I would be glad if it was necessary), I wrote a sample reproducing the problem. The class extends Application and overrides the start(...) method as such:
@Override public void start(Stage primaryStage) throws Exception { final StackPane root = new StackPane(); final Scene scene = new Scene(root, 400, 600); final ListView<Boolean> listView = new ListView<>(); listView.setCellFactory(this::cellFactory); for (int i = 0; i < 5 ; i++) { listView.getItems().add(true); listView.getItems().add(false); } root.getChildren().add(listView); primaryStage.setScene(scene); primaryStage.setTitle("ListView trims the invisible"); primaryStage.show(); }
where this::cellFactory is
private ListCell<Boolean> cellFactory(ListView<Boolean> listView) { return new ListCell<Boolean>() { @Override protected void updateItem(Boolean item, boolean empty) { super.updateItem(item, empty); if (empty || item == null) { setText(null); } else { final Rectangle tabShape = new Rectangle(); tabShape.setHeight(20); tabShape.setWidth(40); tabShape.setVisible(item); final Label label = new Label(item.toString()); label.setLayoutX(40); final Group cellRoot = new Group(); cellRoot.getChildren().add(tabShape); cellRoot.getChildren().add(label); setGraphic(cellRoot); } } }; }
In the above example, a ListView<Boolean> will be displayed with black shapes in front of the true elements (due to the bits of tabShape.setVisible(item); ). false elements look like regular Label objects, as if there was no invisible form in their Group (but it is).
Close comments
Debugging this, it turns out that groups with invisible shapes get negative values ββfor the layoutX property . Therefore, the Label controls are not aligned as desired. This does not happen when I call setLayoutX and setLayoutY outside of the ListView (invisible shapes force offsets), but this is probably not the only place where this will happen.
What is happening and how to avoid it? Alternatively, how do I assume that I am approaching this wrong , which would be the right way? In other words, what is the question I should ask instead?