KotlinとJavaFX 2.2を連携させる
まずIDEはIntelliJ IDEA 12 Community Editionです。
KotlinのインストールはIntelliJにKotlinのプラグインをインストールするだけなのでこちらも簡単です。
”KotlinとJavaFX 2.2を連携させる”という事で既存のFXMLファイルを使ったJavaFX 2.2 WebWiew BrowserのメインクラスをKotlinに書き換え
Kotlinメインクラス起動=>FXMLファイル読み込み=>コントローラクラス(Javaクラス)呼び出し=>アプリ表示
という感じで行きたいとおもいます。
Kotlinに書き換える前のJavaメインクラスはこんな感じです。
WebViewFXML.java
import java.io.IOException; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.scene.layout.AnchorPane; import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.stage.StageStyle; public class WebViewFXML extends Application { public static void main(String[] args) { Application.launch( WebViewFXML.class, args); } @Override public void start(Stage primaryStage) { AnchorPane ap = null; try{ ap = (AnchorPane) FXMLLoader.load(getClass().getClassLoader().getResource("WebViewFXML.fxml")); }catch (IOException ioe){ ioe.printStackTrace(); } primaryStage.setWidth(655); primaryStage.setHeight(600); Scene scene = new Scene(ap); primaryStage.setScene(scene); primaryStage.setX(0.0); primaryStage.setY(0.0); primaryStage.show(); } }
FXMLを使っているのでメインクラスは簡潔です。
次にKotlinで書き換えたメインクラスです。
WebViewFXML.kt
package webviewbrowser import javafx.application.Application import javafx.fxml.FXMLLoader import javafx.scene.Scene import javafx.scene.layout.AnchorPane import javafx.scene.paint.Color import javafx.stage.Stage import javafx.stage.StageStyle fun main(args: Array<String>) = Application.launch(WebViewFXML().javaClass) public class WebViewFXML : Application() { override public fun start(p0: Stage?) { val url:java.net.URL? = getClass().getResource("/WebViewFXML.fxml") val fxmlLoader:FXMLLoader = FXMLLoader(url) val ap: AnchorPane = fxmlLoader.load() as AnchorPane val stage = p0!! (fxmlLoader.getController(): WebViewFXMLController? as WebViewFXMLController).setStage(stage) stage.setWidth(655.0) stage.setHeight(600.0) val scene = Scene(ap) stage.setScene(scene) stage.setX(0.0) stage.setY(0.0) stage.setTitle("WebViewFXML Kotlin") stage.show() } }
WebViewFXML.fxmlはScene Builder 1.1で作れば楽勝です。
<?xml version="1.0" encoding="UTF-8"?> <?import java.lang.*?> <?import java.util.*?> <?import javafx.collections.*?> <?import javafx.geometry.*?> <?import javafx.scene.control.*?> <?import javafx.scene.input.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.paint.*?> <?import javafx.scene.web.*?> <AnchorPane id="AnchorPane" fx:id="ap" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="655.0" xmlns:fx="http://javafx.com/fxml" fx:controller="WebViewFXMLController"> <children> <VBox fx:id="vb" prefHeight="56.0" prefWidth="655.0" AnchorPane.bottomAnchor="544.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> <children> <HBox fx:id="hb1" prefHeight="24.0" prefWidth="655.0"> <children> <MenuBar fx:id="mb"> <menus> <Menu mnemonicParsing="false" text="ファイル" fx:id="m_file"> <items> <MenuItem mnemonicParsing="false" onAction="#mi_open" text="開く" fx:id="mi_open"> <accelerator> <KeyCodeCombination alt="UP" code="O" control="UP" meta="DOWN" shift="UP" shortcut="UP" /> </accelerator> </MenuItem> </items> </Menu> <Menu mnemonicParsing="false" text="編集" fx:id="m_hensyuu"> <items> <MenuItem mnemonicParsing="false" onAction="#mi_sakujyo" text="削除" fx:id="mi_sakujyo" /> </items> </Menu> <Menu mnemonicParsing="false" text="ヘルプ" fx:id="m_help"> <items> <MenuItem mnemonicParsing="false" onAction="#mi_version" text="バージョン情報" fx:id="mi_version" /> </items> </Menu> </menus> </MenuBar> <ChoiceBox fx:id="cb"> <items> <FXCollections fx:factory="observableArrayList"> <String fx:value="Oracle JavaFX" /> <String fx:value="Apple" /> <String fx:value="Google" /> <String fx:value="Youtube" /> <String fx:value="Hinet" /> <String fx:value="JavaFX 2 Documentation" /> <String fx:value="SVG Girl" /> </FXCollections> </items> <HBox.margin> <Insets left="5.0" top="2.0" /> </HBox.margin> </ChoiceBox> </children> </HBox> <HBox fx:id="hb2" prefHeight="32.0" prefWidth="-1.0"> <children> <TextField fx:id="tf" maxWidth="-1.0" minHeight="-Infinity" onAction="#tfAction" prefHeight="22.0" prefWidth="-1.0" HBox.hgrow="ALWAYS"> <HBox.margin> <Insets bottom="5.0" left="15.0" right="15.0" top="5.0" /> </HBox.margin> </TextField> </children> </HBox> </children> </VBox> <Region fx:id="region" prefHeight="544.0" prefWidth="655.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="56.0" /> <WebView fx:id="webview" prefHeight="544.0" prefWidth="655.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="56.0" /> </children> </AnchorPane>
この他にWebViewFXMLController.javaがあります。
起動するとこんな感じです。