0

我有一个带有自定义 ListCellFactory 的 ListView。在我的 CustomListCell 实现中,我使用 setGraphic 将列表项图形设置为 GridPane,但我不能不让 GridPane 填充 ListView 的整个宽度。以下是我的代码的简短版本:

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

    public MyListCell()
    {
        ...
        setContentDisplay(ContentDisplay.GRAPHIC_ONLY); // does not help
        //this.setPrefWidth(0); // does not help
        myComponent.prefWidthProperty().bind(this.widthProperty()); // does not help
    }

    ...

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

即使使用另一个帖子的 prefWidth 边界,我的 GridPane 也不会增长!无论我使用哪种布局约束组合,它看起来都如下所示:

示例视图

GridPane 应扩展到 ListCell 的整个宽度(我想在右下角添加一个删除按钮)。我快疯了,求救!

4

1 回答 1

1

使用一点 css,您可以检查GridPane是否实际使用了单元格中的所有可用空间,但您的控件不是:

public MyListCell(){
    ... 
    _myComponent.setStyle("-fx-border-color: black;");
}

所以你需要选择你想要占用所有可用空间的列,并强制它使用它。

假设您Label在第一个单元格中有一个,在第二个单元格中有一个Button,应该向右对齐。您需要做的就是ColumnConstraints为网格的第二列创建一个新列,并将其设置为始终增长并将内容向右对齐:

private final Label label = new Label();
private final Button button = new Button("Click");

public MyListCell(){
    _myComponent.add(label, 0, 0);
    ColumnConstraints c1 = new ColumnConstraints();

    _myComponent.add(button, 1, 0);
    ColumnConstraints c2 = new ColumnConstraints();
    c2.setHgrow(Priority.ALWAYS);
    GridPane.setHalignment(button, HPos.RIGHT);

    _myComponent.getColumnConstraints().addAll(c1,c2);
}

您将拥有自定义列表单元格:

自定义列表单元

于 2015-01-10T21:27:21.703 回答