I translated a simple HelloWorld application provided by Oracle to Scala (http://docs.oracle.com/javase/8/javafx/get-started-tutorial/hello_world.htm).
It compiles, but gives the above RunTime Exception when I run it. I looked over the 2 other posts on this error via Google, and neither seem to solve my problem:
- One says that I must provide an argument-less constructor when extending
Application
, but Scala creates this automatically. I tried creating one explicitly, but I got the expected "ambiguous occurrence" error. - The other one says that the class extending Application must be public, but again, that shouldn't be a problem given classes are by default
public
If I change object Tests extends Application
to class Tests extends Application
, Eclipse can't "find" the entry point, and gives me the error "Selection does not contain a main type". What's the problem? I hope it's not a compatibility issue.
I downloaded both Scala and Java fairly recently (a month or 2 ago), so I should have the latest versions of each.
Any tips here would be appreciated.
package javaFXTest
import javafx._
import application.Application
import stage.Stage
import scene.Scene
import scene.control.Button
import scene.layout.StackPane
import event.ActionEvent
import event.EventHandler
object Tests extends Application {
val buttonPressHandler = new EventHandler[ActionEvent] {
def handle(event: ActionEvent) = {
println { event }
}
}
//Main entry point
def start(primaryStage: Stage) = {
val button = new Button
button setText "Hello World!"
button setOnAction buttonPressHandler
val root = new StackPane
root.getChildren.add(button)
val scene = new Scene(root, 300, 300)
primaryStage setTitle "Hello World!"
primaryStage setScene scene
primaryStage.show()
}
def main(args: Array[String]) = {
// `:_*` turns a scala array into a java vararg
Application launch (args:_*)
}
I just realized after posting this that because I'm using an object
, it can't be initialized, and won't have a constructor (whoops. My previous constructor test was when I tried using class
instead of object
). So the question is now, why can't Eclipse figure out how to run the above code?
}