Electric Coffee
Electric Coffee

Reputation: 12104

JavaFX in Scala can't find launch method

Trying to see if I can make and run a JavaFX program in Scala I've run into a curious problem, the launch method can't be found...

Here's the Java code:

package example;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.*;
import javafx.stage.*;

public class Program extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("Test.fxml"));

        Scene scene = new Scene(root, 300, 275);

        stage.setTitle("FXML Test");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

And the direct-to-scala translation:

package example

import javafx.application.Application
import javafx.fxml.FXMLLoader
import javafx.scene._
import javafx.stage._

object Program extends Application {
  override def start(stage: Stage): Unit = {
    val root = FXMLLoader.load(getClass getResource "Test.fxml")

    val scene = new Scene(root, 300, 275)

    stage setTitle "FXML Test Scala"
    stage setScene scene
    stage.show
  }

  def main(args: Array[String]): Unit = launch(args) // this bit fails
}

I chose to make a 1:1 translation to see if it'd even work to begin with, but like I said before, the compiler doesn't know what launch is in the Scala one...

So what am I doing wrong here? And more importantly, how do I resolve this?

Upvotes: 1

Views: 328

Answers (1)

Electric Coffee
Electric Coffee

Reputation: 12104

Asked over on the Google+ Scala group and got the following working solution thanks to Witold Czaplewski:

package example

import javafx.application.Application
import javafx.fxml.FXMLLoader
import javafx.scene._
import javafx.stage._

object Program {
  def main(args: Array[String]): Unit = 
    Application.launch(classOf[Program], args: _*)
}

class Program extends Application {
    override def start(stage: Stage): Unit = {
    val root = FXMLLoader.load(getClass() getResource "Test.fxml")
    val scene = new Scene(root, 300, 275)
    stage setTitle "FXML Test Scala"
    stage setScene scene
    stage.show
  }
}

Upvotes: 3

Related Questions