これはいい!JRubyFX+SceneBuilder(fxml)を使ったGUI開発
JRubyFXはJavaFxをRubyっぽく書けますが、
そこにプラスしてfxmlを使うとよりGUI開発が簡単になります。
ここではJRubyFXでfxmlを使う方法をメモとして残します。
まずはJRubyでfxmlをロードする
Scene Builderを使って、適当にhello.fxmlというファイルを作成したあと、
JRubyFXにfxmlを読み込まそうとすると以下のようになります。
require 'jrubyfx' require 'jrubyfx-fxmlloader' fxml_root File.dirname(__FILE__) class Main < JRubyFX::Application def start(stage) with(stage, title: "sample", fxml: SimpleController ) do end.show end end class SimpleController include JRubyFX::Controller fxml "hello.fxml" end Main.launch
次にonActionでラベルに表示してみる
記述を改良します。
require 'jrubyfx' require 'jrubyfx-fxmlloader' fxml_root File.dirname(__FILE__) class Main < JRubyFX::Application def start(stage) with(stage, title: "sample", fxml: SimpleController ) do end.show end end class SimpleController include JRubyFX::Controller fxml "hello.fxml" # fxmlの「onAction="#click_btn"」対応する on :click_btn do puts "pushed!!!" @view.text = "Pushed!!!" # fxml1の「fx:id="view"」に対応する end end Main.launch
SceneBuilderでButtonを作成したあと、SceneBuilder上の右側の情報「Code:」 の中から「On Action」に"click_btn"を書きます。
つぎに、SceneBuilderでLabelを作成したあと、SceneBuilder上の右側の情報「Code:」の中から「fx:id」に"view"を書きます。
このとき、単なる"id"と間違えないようにしてください(「Properties」のほうの"id"じゃないです)