JavaFX ListCell enable horizontal Growth

青春壹個敷衍的年華 提交于 2019-12-08 13:48:24

问题


I currently have a ListView that is resizable in Width and I use a custom CellFactory that returns my custom ListCell objects.

I already read this: Customize ListView in JavaFX with FXML and I use a similar construct like the one mentioned:

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

I want myComponent to take the full size that is available inside the ListCell, however it seems that the setGraphic method limits the size of the given Node.

I can provide all my code if it is necessary, however I am not sure which parts are relevant and I do not want to post a big wall of code.


回答1:


Try setting prefWidth property of your control to Infinity either via CSS or via API so as to provide its maximal expansion whithin container:

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



回答2:


I solved my Problem now, here is how:

class MyListCell extends ListCell<MyObject>
{
    private final AnchorPane _myComponent;

    public MyListCell()
    {
        ...
        this.setPrefWidth(0);
        myComponent.prefWidthProperty().bind(this.widthProperty());
    }

    ...

    @Override
    protected void updateItem(MyObject item, boolean empty)
    {
        ...
        setGraphic(_myComponent);
        ...
    }
}

If this.setPrefWdith(0) is ommited, myComponent will grow inside the ListCell, but the Cell won't shrink if I shrink my ListView.



来源:https://stackoverflow.com/questions/24108466/javafx-listcell-enable-horizontal-growth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!