JavaFX ListCell启用横向增长

我目前有一个可在Width中resize的ListView,我使用一个返回我的自定义ListCell对象的自定义CellFactory。

我已经读过: 使用FXML在JavaFX中自定义ListView ,我使用类似于上面提到的结构:

public class ListViewCell extends ListCell{ @Override public void updateItem(String string, boolean empty){ super.updateItem(string,empty); if(string != null) { ... setGraphic(myComponent); } } 

我希望myComponent采用ListCell中可用的完整大小,但似乎setGraphic方法限制了给定Node的大小。

如果有必要,我可以提供我的所有代码,但是我不确定哪些部分是相关的,我不想发布一大堆代码。

尝试通过CSS或通过API将控件的prefWidth属性设置为Infinity ,以便在容器中提供其最大扩展:

 myComponent.setStyle("-fx-pref-width: Infinity"); 

我现在解决了我的问题,方法如下:

 class MyListCell extends ListCell { private final AnchorPane _myComponent; public MyListCell() { ... this.setPrefWidth(0); myComponent.prefWidthProperty().bind(this.widthProperty()); } ... @Override protected void updateItem(MyObject item, boolean empty) { ... setGraphic(_myComponent); ... } } 

如果this.setPrefWdith(0) ,myComponent将在ListCell内增长,但如果我缩小ListView,Cell将不会缩小。