1

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? }

Community
  • 1
  • 1
Carcigenicate
  • 35,934
  • 8
  • 48
  • 81
  • To make things easier [ScalaFX](http://www.scalafx.org/) exists, and it seems like it's come a long way since I used it. – ggovan Feb 03 '15 at 02:43
  • Do you know any guides on how to just install it? I just spent the last 4 hours this morning trying to install it, and after having installed both Git and SBT (which were supposed to help), I'm just more confused. – Carcigenicate Feb 03 '15 at 18:54

1 Answers1

1

Thanks to this answer, I got it working. The key things to note:

  • The class extending Application must be a class, not an object (duh, in retrospect)
  • Put the main in a companion object of the same name
  • Application launch (args:_*) needs to be Application.launch(classOf[Tests],args:_*) (where Tests = Your class).

In the end, this launched correctly:

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


class 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()


    }

}

object Tests {

    def main(args: Array[String]) = {
        // `:_*` turns a scala array into a java vararg
        Application.launch(classOf[Tests],args:_*)
    }

}

Hopefully this helps.

Community
  • 1
  • 1
Carcigenicate
  • 35,934
  • 8
  • 48
  • 81