Is there some guide or walkthrough to building a Scala + JavaFX desktop application?
I'm having hard time finding a good source and I am using IntelliJ IDEA as the IDE.
Even the most simplistic desktop hello world samples would help a lot, because I have little clue where to start.
Update: This is what I have now:
import javafx.application.Application
import javafx.scene.Scene
import javafx.scene.layout.StackPane
import javafx.stage.Stage
import javafx.scene.control.Label
class Test extends Application {
override def start(primaryStage: Stage) {
primaryStage.setTitle("Sup!")
val root = new StackPane
root.getChildren.add(new Label("Hello world!"))
primaryStage.setScene(new Scene(root, 300, 300))
primaryStage.show()
}
}
object Test {
def main(args: Array[String]) {
val t = new Test
t.start(new Stage)
}
}
Running it I get:
Exception in thread "main" java.lang.IllegalStateException: Not on FX application thread; currentThread = main
How can I get it to display the hello world window with the label?
JavaFX is a great UI toolkit to write Cross-platform desktop applications. With https://gluonhq.com/products/mobile/ support, it is even possible to write mobile applications.
There are a few things to know when writing Scala based JavaFX applications.
First, here's a sample hello world app:
import javafx.application.Application
import javafx.scene.Scene
import javafx.scene.layout.StackPane
import javafx.stage.Stage
import javafx.scene.control.Label
class Test extends Application {
println("Test()")
override def start(primaryStage: Stage) {
primaryStage.setTitle("Sup!")
val root = new StackPane
root.getChildren.add(new Label("Hello world!"))
primaryStage.setScene(new Scene(root, 300, 300))
primaryStage.show()
}
}
object Test {
def main(args: Array[String]) {
Application.launch(classOf[Test], args: _*)
}
}
Running it you should get:
Here's an official hello world example in Java: http://docs.oracle.com/javafx/2/get_started/hello_world.htm
The main differences are:
def main()
that launches the actual application.Application.launch(classOf[Test], args: _*)
.If you just try to run the application directly with Application.launch(args : _*)
you will get this error:
Exception in thread "main" java.lang.RuntimeException: Error: class Test$ is not a subclass of javafx.application.Application
To learn more about JavaFX, just read the official documentation: http://docs.oracle.com/javafx/index.html
You can use this way.
class BuildFx extends Application{
override def start(primaryStage: Stage): Unit = {
primaryStage.setTitle("Scala")
var btn=new Button("Say Hello Scala")
val root=new StackPane()
root.getChildren().add(btn)
primaryStage.setScene(new Scene(root, 300, 300))
primaryStage.show()
}
def launchIt():Unit={
Application.launch()
}
}
///////////////////////////////////////////////////////////
object Init{
def main(args: Array[String]): Unit = {
val buildFx=new BuildFx
buildFx.launchIt()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With