ListView与自定义ListCell

时间:2016-06-30 20:27:56

标签: javafx scalafx

我正在学习scalaFX / JavaFX,所以作为一个热身,并且是一个大项目的一部分,我正在编写一个远程文件选择器。将ListView绑定到ObservableBuffer[File]。但我不希望File.toString作为文字,因此我不想延长File而是使用ListCell。我没有找到任何这样做的例子,所以我认为我发布了这个。

这没关系,但我收到了Null个文件,然后在跳过Null时,我仍然在列表中有旧文件(如果在更改之前有x个文件,x-之后,你有旧数据的单元格)。

最后,我将""用于空文件,并从ListView中删除丢失的文件项。我在ListView API中没有看到任何提及此问题的内容。

这对你来说是否合适?在我看来应该有另一个地方去除细胞。默认cellFactory处理得很好。

val fileList = new ListView[File](fileSystemModel.fileList) {
      cellFactory = { _ =>
        new ListCell[File] {
          item.onChange {
            (_, _, file) =>
              if (file != null) {
                text = s"${file.getName}${if (file.isDirectory) "/" else ""}"
              } else text = ""
          }
        }
      }
    }

1 个答案:

答案 0 :(得分:0)

这非常奇怪,但我做到了:

import javafx.scene.{control => jfxsc}

case class Item(name: String)

items = ... //ListView[Item]

items.cellFactory = (lv: ListView[Item]) => {
    new ListCell(new jfxsc.ListCell[Item] {
    override def updateItem(item: Item, empty: Boolean): Unit = {
        if (empty || item == null) {
            setText(null)
            setGraphic(null)
        } else {
            setText(item.name)
        }
    }
    })
}
相关问题