ScalaFXでJavaFX 2のFXMLLoaderを使う

ScalaFXでJavaFX 2のFXMLLoaderを使うの自体は簡単です。
後はコントローラーをどのように書くかですね2種類の方法があります。
まず方法1はJavaFXで書く
方法2はScalaFXで書くです
方法2についてはProScalaFXにサンプルがあります。
実行するとこんな感じです。

ProScalaFXのサンプルと同じようにコントローラーをScalaFXで書いてSystem.getPropertyで得られる値を一部GridPaneの表に表示する簡単なアプリを作ってみます。
今回もIntellij IDEA 12.1.6 ceを使います。
1. FileメニューよりNew Project...を選択。
プロジェクト名(GetProperty)とScala Homeを記入

2. Fileメニューから”Project Structure...”を選択
Project Settingsの中のLibrariesを選択

3. 左上の"+" をクリック
scalafx_2.10-1.0.0-M6.jarを追加する。
最新のjarはここにあります。
http://search.maven.org/#search%7Cga%7C1%7Cscalafx

4. srcフォルダをcontrolキー+クリック又はマウスの右クリックでNew=>Scala Classを作成します。
作成するのは次の2つのクラスです。
GetProperty.scala
GetPropertyController.scala


GetProperty.scala

/**
 * Created with Intellij IDEA.
 * User: hshino
 * Date: 13/10/31
 * Time: 18:30
 * To change this template use File | Settings | File Templates.
 */
import java.io.IOException
import javafx.{fxml => jfxf}
import javafx.{scene => jfxs}
import scalafx.Includes._
import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.scene.Scene

/** Example of using FXMLLoader from ScalaFX.
  */
object GetProperty extends JFXApp {

  val resource = getClass.getResource("GetProperty.fxml")
  if (resource == null) {
    throw new IOException("Cannot load resource: GetProperty.fxml")
  }

  val root: jfxs.Parent = jfxf.FXMLLoader.load(resource)

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

}

GetPropertyController.scala

/**
 * Created with Intellij IDEA.
 * User: hshino
 * Date: 13/10/31
 * Time: 18:32
 * To change this template use File | Settings | File Templates.
 */
import java.net.URL
import java.util
import javafx.scene.{control => jfxsc}
import javafx.{event => jfxe}
import javafx.{fxml => jfxf}
import scalafx.scene.control.TextField

class GetPropertyController extends jfxf.Initializable {

  @jfxf.FXML private var tfDelegate1: jfxsc.TextField = null
  @jfxf.FXML private var tfDelegate2: jfxsc.TextField = null
  @jfxf.FXML private var tfDelegate3: jfxsc.TextField = null
  @jfxf.FXML private var tfDelegate4: jfxsc.TextField = null
  @jfxf.FXML private var tfDelegate5: jfxsc.TextField = null
  @jfxf.FXML private var tfDelegate6: jfxsc.TextField = null
  @jfxf.FXML private var tfDelegate7: jfxsc.TextField = null
  @jfxf.FXML private var tfDelegate8: jfxsc.TextField = null
  @jfxf.FXML private var tfDelegate9: jfxsc.TextField = null
  @jfxf.FXML private var tfDelegate10: jfxsc.TextField = null
  @jfxf.FXML private var tfDelegate11: jfxsc.TextField = null
  @jfxf.FXML private var tfDelegate12: jfxsc.TextField = null
  private var tf1: TextField = _
  private var tf2: TextField = _
  private var tf3: TextField = _
  private var tf4: TextField = _
  private var tf5: TextField = _
  private var tf6: TextField = _
  private var tf7: TextField = _
  private var tf8: TextField = _
  private var tf9: TextField = _
  private var tf10: TextField = _
  private var tf11: TextField = _
  private var tf12: TextField = _


  @jfxf.FXML
  private def handleButtonAction(event: jfxe.ActionEvent) {
    System.out.println("You clicked me!")

    tf1.setText(System.getProperty("java.vendor"))
    tf2.setText(System.getProperty("java.vendor.url"))
    tf3.setText(System.getProperty("java.home") )
    tf4.setText(System.getProperty("java.awt.graphicsenv"))
    tf5.setText(System.getProperty("java.class.version"))
    tf6.setText(System.getProperty("sun.boot.class.path"))
    tf7.setText(System.getProperty("java.version"))
    tf8.setText(System.getProperty("javafx.runtime.version"))
    tf9.setText(System.getProperty("java.runtime.version"))
    tf10.setText(System.getProperty( "os.name"))
    tf11.setText(System.getProperty( "os.arch" ))
    tf12.setText(System.getProperty("os.version" ))
  }


  def initialize(url: URL, rb: util.ResourceBundle) {
    tf1 = new TextField(tfDelegate1)
    tf2 = new TextField(tfDelegate2)
    tf3 = new TextField(tfDelegate3)
    tf4 = new TextField(tfDelegate4)
    tf5 = new TextField(tfDelegate5)
    tf6 = new TextField(tfDelegate6)
    tf7 = new TextField(tfDelegate7)
    tf8 = new TextField(tfDelegate8)
    tf9 = new TextField(tfDelegate9)
    tf10 = new TextField(tfDelegate10)
    tf11 = new TextField(tfDelegate11)
    tf12 = new TextField(tfDelegate12)
  }
}

5. fxmlファイルはSceneBuilder 1.1で作りIDEAのプロジェクトのフォルダに保存します。


GetProperty.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<AnchorPane id="anchorPane" prefHeight="500.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="GetPropertyController">
  <children>
    <VBox padding="$x2" prefHeight="500.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
      <children>
        <Button mnemonicParsing="false" onAction="#handleButtonAction" text="Click me!" textFill="RED">
          <font>
            <Font size="16.0" fx:id="x1" />
          </font>
        </Button>
        <GridPane prefHeight="382.0" prefWidth="565.0">
          <children>
            <Label alignment="CENTER" contentDisplay="CENTER" font="$x1" prefHeight="28.0" prefWidth="280.0" text="java.vendor" textAlignment="LEFT" textFill="#0029ff" textOverrun="ELLIPSIS" underline="false" wrapText="false" GridPane.columnIndex="0" GridPane.rowIndex="0" />
            <Label alignment="CENTER" font="$x1" prefHeight="28.0" prefWidth="280.0" text="java.vendor.url" textFill="#33ffe7" GridPane.columnIndex="0" GridPane.rowIndex="1" />
            <Label alignment="CENTER" font="$x1" prefHeight="28.0" prefWidth="280.0" text="java.home" textFill="#ff9533" GridPane.columnIndex="0" GridPane.rowIndex="2" />
            <Label alignment="CENTER" font="$x1" prefHeight="28.0" prefWidth="280.0" text="java.awt.graphicsenv" textFill="#ff66af" GridPane.columnIndex="0" GridPane.rowIndex="3" />
            <Label alignment="CENTER" font="$x1" prefHeight="28.0" prefWidth="280.0" text="java.class.version" textFill="#00cc83" GridPane.columnIndex="0" GridPane.rowIndex="4" />
            <Label alignment="CENTER" font="$x1" prefHeight="28.0" prefWidth="280.0" text="sun.boot.class.path" textFill="#0c9900" GridPane.columnIndex="0" GridPane.rowIndex="5" />
            <Label alignment="CENTER" font="$x1" prefHeight="28.0" prefWidth="280.0" text="java.version" textFill="#1400ff" GridPane.columnIndex="0" GridPane.rowIndex="6" />
            <Label alignment="CENTER" font="$x1" prefHeight="28.0" prefWidth="280.0" text="javafx.version" textFill="#66edff" GridPane.columnIndex="0" GridPane.rowIndex="7" />
            <Label alignment="CENTER" font="$x1" prefHeight="28.0" prefWidth="280.0" text="java.runtime.version" textFill="#ffb800" GridPane.columnIndex="0" GridPane.rowIndex="8" />
            <Label alignment="CENTER" font="$x1" prefHeight="28.0" prefWidth="280.0" text="os.name" textFill="#ff00b8" GridPane.columnIndex="0" GridPane.rowIndex="9" />
            <Label alignment="CENTER" font="$x1" prefHeight="28.0" prefWidth="280.0" text="os.arch" textFill="#00e0ff" GridPane.columnIndex="0" GridPane.rowIndex="10" />
            <Label alignment="CENTER" font="$x1" prefHeight="28.0" prefWidth="280.0" text="os.version" textFill="#00cc21" GridPane.columnIndex="0" GridPane.rowIndex="11" />
            <TextField fx:id="tfDelegate1" prefHeight="32.0" prefWidth="280.0" GridPane.columnIndex="1" GridPane.rowIndex="0" />
            <TextField fx:id="tfDelegate2" prefHeight="32.0" prefWidth="280.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
            <TextField fx:id="tfDelegate3" prefHeight="32.0" prefWidth="280.0" GridPane.columnIndex="1" GridPane.rowIndex="2" />
            <TextField fx:id="tfDelegate4" prefHeight="32.0" prefWidth="280.0" GridPane.columnIndex="1" GridPane.rowIndex="3" />
            <TextField fx:id="tfDelegate5" prefHeight="32.0" prefWidth="280.0" GridPane.columnIndex="1" GridPane.rowIndex="4" />
            <TextField fx:id="tfDelegate6" prefHeight="32.0" prefWidth="280.0" GridPane.columnIndex="1" GridPane.rowIndex="5" />
            <TextField fx:id="tfDelegate7" prefHeight="32.0" prefWidth="280.0" GridPane.columnIndex="1" GridPane.rowIndex="6" />
            <TextField fx:id="tfDelegate8" prefHeight="32.0" prefWidth="280.0" GridPane.columnIndex="1" GridPane.rowIndex="7" />
            <TextField fx:id="tfDelegate9" prefHeight="32.0" prefWidth="280.0" GridPane.columnIndex="1" GridPane.rowIndex="8" />
            <TextField fx:id="tfDelegate10" prefHeight="32.0" prefWidth="280.0" GridPane.columnIndex="1" GridPane.rowIndex="9" />
            <TextField fx:id="tfDelegate11" prefHeight="32.0" prefWidth="280.0" GridPane.columnIndex="1" GridPane.rowIndex="10" />
            <TextField fx:id="tfDelegate12" prefHeight="32.0" prefWidth="280.0" GridPane.columnIndex="1" GridPane.rowIndex="11" />
          </children>
          <columnConstraints>
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          </columnConstraints>
          <rowConstraints>
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          </rowConstraints>
          <VBox.margin>
            <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" fx:id="x2" />
          </VBox.margin>
        </GridPane>
      </children>
    </VBox>
  </children>
</AnchorPane>

6. 後はRunメニューからRunさせてください。
Click me!ボタンをクリックするとPropertyを表示します。