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

时间:2017-07-20 11:46:38

标签: java scala javafx treeview scalafx

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

与上面相同的错误消息。

1 个答案:

答案 0 :(得分:0)

<强>解决方案: 我忘记了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

}