ScalaFX i18n with fxml not working

时间:2015-07-31 19:47:23

标签: scala internationalization fxml scalafx fxmlloader

First the working code:

val root: BorderPane = new BorderPane(jfxf.FXMLLoader.load(getClass.getResource("/GUI/main.fxml")))

stage = new PrimaryStage()
{
  title = "FXML Test"
  scene = new Scene(root)
}

No problem here. Now I wanted to add i18n support like so:

val bundle: ResourceBundle = new PropertyResourceBundle(getClass.getResource("/i18n/en.properties").openStream)
val loader: FXMLLoader = new FXMLLoader(getClass.getResource("/GUI/main.fxml"), bundle)
val root = loader.load[jfxs.Parent]

stage = new PrimaryStage()
{
  title = "FXML Test"
  scene = new Scene(root)
}

Now the constructor scene = new Scene(root) cannot be resolved.

I tried to solve this by

1) initializing a new BorderPane, like:

val root = new BorderPane(loader.load[jfxs.Parent])

But the constructor of BorderPane cannot be resolved so I tried

2) casting it to a BorderPane, like:

val root = new BorderPane(loader.load[jfxs.Parent].asInstanceOf[BorderPane])

which is okay in the IDE but throws a compiler-error:

Caused by: java.lang.ClassCastException: javafx.scene.layout.BorderPane cannot be cast to scalafx.scene.layout.BorderPane

How can I resolve this?

1 个答案:

答案 0 :(得分:1)

"现在构造函数scene = new Scene(root)无法解析。":那是因为scalafx.scene.Scene构造函数需要scalafx.scene.Parent类型的参数而不是{{1 }}

只需在导入中添加javafx.scene.Parent即可使用ScalaFX的隐式转换。然后你可以这样做:

import scalafx.Includes._