简体   繁体   中英

Are the children of a ScalaFX Pane of JavaFX type?

I am quite new to Scala and ScalaFX, so I probably do not understand this correctly. My issue: I add a node (fe Button) to a ScalaFX Pane (fe VBox). When I access the children of the Pane the children type changed from ScalaFX Button to JavaFX Button.

When I have a look at the source code of ScalaFX I can see that everything is done with delegates. So is the ScalaFX node lost? I think there is done some magic (for me as a newbie) with implicit conversion from ScalaFX to JavaFX. Is it possible to do an implicit conversion the other way round? Do I use ScalaFX correctly?

I wanted to go through all children of a Pane. When a child is from special type I wanted to perform some operations with this child. Now I would have to check for JavaFX type and work on a JavaFX node, which does not appears like clean code to me.

Any commments welcome. Thanks in advance.

I attached a running example.

import scalafx.Includes._
import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.scene.Scene
import scalafx.scene.control.Button
import scalafx.scene.layout.VBox
import scalafx.stage.WindowEvent

object TestChildren extends JFXApp {
  stage = new PrimaryStage {
    val contentPane = new VBox()
    val b1 = new Button("B1")
    contentPane.children.add(b1)
    scene = new Scene (contentPane)

    println(b1.getClass)
    println("--")
    for (child <- contentPane.children) {
      println(child.getClass)
    }
  }
}

Yes, there is an implicit conversion. I do not know which IDE you are using, but for example IntelliJ 14.1 with the Scala plugin indicates implicit conversions, which is a really useful feature.

In your case,

contentPane.children.add(b1)

is the line where the implicit conversion happens. The explicit form is

contentPane.children.add(Button.sfxButton2jfx(b1))

where the implicit conversion is a one-liner in scalafx.scene.control.Button :

implicit def sfxButton2jfx(v: Button): jfxsc.Button = if (v != null) v.delegate else null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM