ListView CellFactory - How to remove cells correctly?

后端 未结 1 1072
长发绾君心
长发绾君心 2021-01-26 11:49

I have a ListView that I am working to add a ContextMenu to. I have the ContextMenu working find but have another issue.

My

1条回答
  •  孤城傲影
    2021-01-26 12:31

    The problem arises from the line

    cell.textProperty().bind(cell.itemProperty().asString());
    

    When the cell is empty, the item will be null, so the binding will (I believe) evaluate to the string "null".

    Try something that tests for the cell being empty or the item being null, e.g.

    cell.textProperty().bind(Bindings
        .when(cell.emptyProperty())
        .then("")
        .otherwise(cell.itemProperty().asString()));
    

    or (thanks to @fabian for refining this version)

    cell.textProperty().bind(Bindings.createStringBinding(
        () -> Objects.toString(cell.getItem(), ""),
        cell.itemProperty()));
    

    0 讨论(0)
提交回复
热议问题