Search code examples
javascalajavafxtreeviewscalafx

Passing parameters to custom TreeItem constructor


Relatively new to Scala and ScalaFX but I've worked with Java and JavaFX before. My question is if there is a way to pass parameters to a custom TreeItem?

Code looks like this:

I'd like to do this:

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

With this:

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

But I get this error message:

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

I tried placing the parameter picture in the class argument list but that does not work either. Like 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)
  )

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

    gridPane.addColumn(1, progressBar)

    gridPane.columnConstraints = columnConstraints
    graphic = gridPane
  }
}

Same error message as above.


Solution

  • Solution: I forget that the default constructor in Scala is outside of the functions so I did not need def this() = this() {...}

    Instead this worked:

        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
    
    }