JavaFX ListCell启用横向增长 [英] JavaFX ListCell enable horizontal Growth

查看:533
本文介绍了JavaFX ListCell启用横向增长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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);
    }
}

我想 myComponent 获取ListCell中可用的完整大小,但似乎 setGraphic 方法限制了给定节点的大小。

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.

推荐答案

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

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);
        ...
    }
}

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

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

这篇关于JavaFX ListCell启用横向增长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆