This code compiles and runs:import scalafx.Includes._import scalafx.application.JFXAppimport scalafx.scene.Sceneimport scalafx.scene.layout.HBoximport scalafx.stage.Stageimport javafx.scene.shape.Circle // ClassCastException with scalafx.scene.shape.Circleclass MyCircle extends Circle { def myMethod="It works!" }object Example extends JFXApp {val myCircle = new MyCircle; myCircle.setRadius(20)val myContent = new HBox { content=myCircle }val myScene = new Scene { content=myContent }val myStage = new Stage { scene=myScene; width=100; height=100 }stage = myStageval javafxCircle = myContent.children.get(0).asInstanceOf[MyCircle]println(javafxCircle.myMethod)}When i use a scalafx.scene.shape.Circle the code compiles but aClassCastException is thrown at runtime.Are children of scalafx classes unretrievable when they are availableonly as a javafx.scene.Node ?Frank
Thanks for the reply, Alain.My example was somewhat misleading but your answerled me to a solution.Below is an updated example that runs fine.I want to write widgets (like the simplified MyCircle)that have complex functionality (like onMouseClicked)and own data (like colorWhenClicked).If i add a scalafx Circle (wrapper) as a child to some Parent thereseems to be no way to access MyCircle data or methods from thejavafx child Nodes that the Parent provides (allthough MyCircle runscorrect when written in that way).But i want to access my widgets by traversing the scene graph.If i extend my widgets from JavaFX classes and wrap them toscalafx in the constructor (sfxThis) the widgets can be writtenwith the advantages of scalafx and are reachable by the scenegraph.Not perfect but the code is better to read than with JavaFX alone.Frank
import scalafx.Includes._import scalafx.application.JFXAppimport scalafx.scene.Sceneimport scalafx.scene.layout.HBoximport scalafx.stage.Stage
import scalafx.scene.paint.Colorimport javafx.scene.input.MouseEventclass MyCircle extends javafx.scene.shape.Circle {val sfxThis = new scalafx.scene.shape.Circle(this)val colorWhenClicked=Color.REDsfxThis.onMouseClicked = { (e: MouseEvent) => sfxThis.fill=colorWhenClicked }def myMethod="colorWhenClicked: " + colorWhenClicked.toString}object Example2 extends JFXApp {
val myCircle = new MyCircle; myCircle.setRadius(20)val myContent = new HBox { content=myCircle }val myScene = new Scene { content=myContent }val myStage = new Stage { scene=myScene; width=100; height=100 }stage = myStage
val circleObtainedFromSceneGraph = myContent.children.get(0).asInstanceOf[MyCircle]println(circleObtainedFromSceneGraph.myMethod)