0

I want to use this code to display generic Javafx Scene from my program

class JFXWindowDisplay(myScene:Scene) extends Application {

    def start( stage: Stage) {

      stage.setTitle("My JavaFX Application")
      stage.setScene(myScene)
      stage.show()
    }
  }

//OBJECT NEEDED TO RUN JAVAFX : http://stackoverflow.com/questions/12124657/getting-started-on-scala-javafx-desktop-application-development
  object JFXWindowDisplay extends App {

    override def main(args: Array[String]) {
      Application.launch(classOf[JFXWindowDisplay], args: _*)
    }
  }

How can i pass Scene argument to the compagnon object ?

For example, i want to create a program which open a new javafx windows from scala script execution of this :

class myProgram() extends App  {

  val root = new StackPane
  root.getChildren.add(new Label("Hello world!"))

  val myScene = new Scene(root, 300, 300))
  // then run ControlTest object with this scene
  ControlTest(myScene)
}
reyman64
  • 523
  • 4
  • 34
  • 73

1 Answers1

0

You could prepare two or more traits each with his specific scene and then mix the one you need to the application class

trait SceneProvider {
  def scene: Scene
}

trait Scene1 extends SceneProvider {
  val root = new StackPane
  root.getChildren.add(new Label("Hello world!"))
  def scene = new Scene(root, 300, 300)
}

trait Scene2 extends SceneProvider {
  val root = new StackPane
  root.getChildren.add(new Label("Hello world 2!"))
  def scene = new Scene(root, 300, 300)
}

trait JFXWindowDisplay {
  self: SceneProvider =>

  def start(stage: Stage) {
    stage.setTitle("My JavaFX Application")
    stage.setScene(scene)
    stage.show()
  }
}


class JFXWindowDisplay1 extends Application with JFXWindowDisplay with Scene1
class JFXWindowDisplay2 extends Application with JFXWindowDisplay with Scene2

object JFXMain extends App {
  override def main(args: Array[String]) {
    //here you chose an application class
    Application.launch(classOf[JFXWindowDisplay1], args: _*)
  }
}

EDIT: now this works, based on your comments.

pagoda_5b
  • 7,333
  • 1
  • 27
  • 40
  • of course you can define the `scene` member as a method in all the `traits` if you don't need a reference to the created instance – pagoda_5b Jan 15 '13 at 22:17
  • Hi, to work, object and class need the same name, so `JFXWindowDisplay` need to be a class, and we also need `classOf[JFXWindowsDisplay]` into objet launcher `JFXWindowDisplay` :( Hard to find answer – reyman64 Jan 16 '13 at 10:45