Top Banner
Scene BuilderFXML Java in the Box 櫻庭 祐一
21

Scene BuilderでFXML

Jan 15, 2015

Download

Technology

skrb

Japan JavaFX User Group seminar on 2 Jul. 2012.
"Editing FXML w/ Scene Builder"
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Scene BuilderでFXML

Scene Builderで FXML

Java in the Box櫻庭 祐一

Page 2: Scene BuilderでFXML
Page 3: Scene BuilderでFXML

FXML

Page 4: Scene BuilderでFXML

JavaFXGUI Library for Java

もちろん、すべて Java で記述

Page 5: Scene BuilderでFXML

public class Hello extends Application {

}

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

@Override public void start(Stage stage) {

}

// ルートコンテナ AnchorPane root = new AnchorPane(); // Scene Graph を生成し、ルートコンテナを貼る Scene scene = new Scene(root); stage.setScene(scene);

stage.show();

// Scene Graph を構築

Page 6: Scene BuilderでFXML

Scene Graph= Tree

Scene Graph= Tree

Page 7: Scene BuilderでFXML

Stage

Scene

AnchorPane

HBoxTableView

Label TextField Button

Page 8: Scene BuilderでFXML

AnchorPane root = new AnchorPane(); Scene scene = new Scene(root); stage.setScene(scene);

TableView<Person> table = new TableView<>(); root.getChildren().add(table);

HBox hbox = new HBox(); Label label = new Label("Label"); hbox.getChildren().add(label); TextField field = new TextField("TextField"); hbox.getChildren().add(field); Button button = new Button("button"); hbox.getChildren().add(button); root.getChildren().add(hbox);

AnchorPane root = new AnchorPane(); Scene scene = new Scene(root); stage.setScene(scene);

TableView<Person> table = new TableView<>(); root.getChildren().add(table);

HBox hbox = new HBox(); Label label = new Label("Label"); hbox.getChildren().add(label); TextField field = new TextField("TextField"); hbox.getChildren().add(field); Button button = new Button("button"); hbox.getChildren().add(button); root.getChildren().add(hbox);

Page 9: Scene BuilderでFXML

Java

手続き 構造

XML

冗長

ツールとの相性

簡潔

ツールとの相性

Page 10: Scene BuilderでFXML

FXMLFXMLシーングラフを XML で表す

スキーマレスクラス : 要素プロパティ : 属性 or 要素

CSS

Java との連携

Page 11: Scene BuilderでFXML

<AnchorPane prefHeight="400.0" prefWidth="600.0"> <children> <HBox alignment="CENTER" prefHeight="50.0" prefWidth="572.0" spacing="20.0"> <children> <Label style="-fx-font-size: 24;" text="Label" /> <TextField style="-fx-font-size: 24;" text="TextField" /> <Button style="-fx-font-size: 24;" text="Load" /> </children> </HBox> <TableView prefHeight="302.0" prefWidth="572.0" /> </children></AnchorPane>

<AnchorPane prefHeight="400.0" prefWidth="600.0"> <children> <HBox alignment="CENTER" prefHeight="50.0" prefWidth="572.0" spacing="20.0"> <children> <Label style="-fx-font-size: 24;" text="Label" /> <TextField style="-fx-font-size: 24;" text="TextField" /> <Button style="-fx-font-size: 24;" text="Load" /> </children> </HBox> <TableView prefHeight="302.0" prefWidth="572.0" /> </children></AnchorPane>

<AnchorPane prefHeight="400.0" prefWidth="600.0"> <children> <HBox alignment="CENTER" prefHeight="50.0" prefWidth="572.0" spacing="20.0"> <children> <Label style="-fx-font-size: 24;" text="Label" /> <TextField style="-fx-font-size: 24;" text="TextField" /> <Button style="-fx-font-size: 24;" text="Load" /> </children> </HBox> <TableView prefHeight="302.0" prefWidth="572.0" /> </children></AnchorPane>

<AnchorPane prefHeight="400.0" prefWidth="600.0"> <children> <HBox alignment="CENTER" prefHeight="50.0" prefWidth="572.0" spacing="20.0"> <children> <Label style="-fx-font-size: 24;" text="Label" /> <TextField style="-fx-font-size: 24;" text="TextField" /> <Button style="-fx-font-size: 24;" text="Load" /> </children> </HBox> <TableView prefHeight="302.0" prefWidth="572.0" /> </children></AnchorPane>

AnchorPane pane = FXMLLoader.load( this.getClass().getResource("table.fxml"));

Page 12: Scene BuilderでFXML

FXML と Java の連携

ModelViewFXML Java

ControllerJava

fx:id#method

@FXML

Page 13: Scene BuilderでFXML

<AnchorPane xmlns:fx="http://javafx.com/fxml" fx:controller="contents.TableController"> <children> <HBox> <children> <Label /> <TextField fx:id="textfield" /> <Button onAction="#handleAction" /> </children> </HBox> <TableView fx:id="table" /> </children></AnchorPane>

<AnchorPane xmlns:fx="http://javafx.com/fxml" fx:controller="contents.TableController"> <children> <HBox> <children> <Label /> <TextField fx:id="textfield" /> <Button onAction="#handleAction" /> </children> </HBox> <TableView fx:id="table" /> </children></AnchorPane>

<AnchorPane xmlns:fx="http://javafx.com/fxml" fx:controller="contents.TableController"> <children> <HBox> <children> <Label /> <TextField fx:id="textfield" /> <Button onAction="#handleAction" /> </children> </HBox> <TableView fx:id="table" /> </children></AnchorPane>public class TableController implements Initializable {

@FXML private TextField textfield; @FXML private TableView table; @FXML public void handleAction(ActionEvent event) { // テーブルの更新 } @Override public void initialize(URL url, ResourceBundle rb) { // テーブルの初期化 } }

public class TableController implements Initializable { @FXML private TextField textfield; @FXML private TableView table; @FXML public void handleAction(ActionEvent event) { // テーブルの更新 } @Override public void initialize(URL url, ResourceBundle rb) { // テーブルの初期化 } }

Page 14: Scene BuilderでFXML

FXML は便利

でも複雑なGUIになると...

やっぱりツールがほしい!!

Scene Builder

Page 15: Scene BuilderでFXML

Scene Builder

Page 16: Scene BuilderでFXML

Scene Builder

WYSWYG FXML Editor

NetBeans Scene BuilderJava FXML

連携

Page 17: Scene BuilderでFXML

できること

FXMLをグラフィカルに編集

Preview

CSSの設定

Controller の設定

Page 18: Scene BuilderでFXML

できないこと

コントローラの作成

外部CSSの編集

FXMLソースの表示

NBとのプロジェクト共有

Page 19: Scene BuilderでFXML

Making an Application w/ Scene Builder

Page 20: Scene BuilderでFXML

Conclusion

FXML でシーングラフを簡単に

MVC (Presentation Model, MVVM)

Scene Builder: FXML Editor

UI 構築には便利

まだまだ足りない点いっぱい

Page 21: Scene BuilderでFXML

Scene Builderで FXML

Java in the Box櫻庭 祐一