将参数传递给自定义 TreeItem 构造函数

Passing parameters to custom TreeItem constructor

Scala 和 ScalaFX 相对较新,但我之前使用过 Java 和 JavaFX。我的问题是是否有办法将参数传递给自定义 TreeItem?

代码如下所示:

我想这样做:

def makePictureHolder(picture: Picture): TreeItem[Picture] = {
    new TemporaryHolderTreeItem(picture)
  }

有了这个:

class TemporaryHolderTreeItem extends TreeItem[Picture] {

  private val gridPane = new GridPane
  private val progressBar = new ProgressBar {
    prefWidth = 250
  }
  private val columnConstraints = ObservableBuffer(
    new ColumnConstraints(500),
    new ColumnConstraints(250)
  )

  def this(picture: Picture) = this() {
    value = picture

    gridPane.addColumn(0, new Label(resourceBundle
      .getString("uploadHolderText") + " " + picture.path))

    gridPane.addColumn(1, progressBar)

    gridPane.columnConstraints = columnConstraints
    graphic = gridPane
  }
} 

但我收到此错误消息:

TemporaryHolderTreeItem.scala:24: com.nodefactory.diehard.gail.views.TemporaryHolderTreeItem does not take parameters
[error]   def this(picture: Picture) = this() {
[error]             

我尝试将参数图片放在 class 参数列表中,但这也不起作用。 像这样:

class TemporaryHolderTreeItem(picture: Picture) extends TreeItem[Picture](picture) {

  private val gridPane = new GridPane
  private val progressBar = new ProgressBar {
    prefWidth = 250
  }
  private val columnConstraints = ObservableBuffer(
    new ColumnConstraints(500),
    new ColumnConstraints(250)
  )

  def this() = this() {
    gridPane.addColumn(0, new Label(resourceBundle
      .getString("uploadHolderText") + " " + picture.path))

    gridPane.addColumn(1, progressBar)

    gridPane.columnConstraints = columnConstraints
    graphic = gridPane
  }
}

与上面相同的错误消息。

解法: 我忘记了 Scala 中的默认构造函数在函数之外,所以我不需要 def this() = this() {...}

相反,这有效:

    class TemporaryHolderTreeItem(picture: Picture) extends TreeItem[Picture](picture) {

  private val gridPane = new GridPane
  private val progressBar = new ProgressBar {
    prefWidth = 250
  }
  private val columnConstraints = ObservableBuffer(
    new ColumnConstraints(500),
    new ColumnConstraints(250)
  )

  gridPane.addColumn(0, new Label(resourceBundle
    .getString("uploadHolderText") + " " + picture.path))

  gridPane.addColumn(1, progressBar)

  gridPane.columnConstraints = columnConstraints
  graphic = gridPane

}