Skip to content

Instantly share code, notes, and snippets.

@aoetk
Last active December 10, 2015 01:58
Show Gist options
  • Save aoetk/4363405 to your computer and use it in GitHub Desktop.
Save aoetk/4363405 to your computer and use it in GitHub Desktop.
JavaFXバインディングのサンプル
package aoetk.bindingsample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
* バインディングのサンプルアプリケーション。
* @author aoetk
*/
public class BindingSample extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* メインメソッド。
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
package aoetk.bindingsample.model;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.property.ReadOnlyStringProperty;
import javafx.beans.property.ReadOnlyStringPropertyBase;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
/**
* モデルの例。
* @author aoetk
*/
public class Person {
// nameプロパティ
private StringProperty name = new SimpleStringProperty();
// 外部に公開するプロパティ
public StringProperty nameProperty() {
return name;
}
// JavaBeans仕様と互換を持たせるためのgetter、setter
public final String getName() {
return name.get();
}
public final void setName(String name) {
this.name.set(name);
}
// messageプロパティ (read only)
private ReadOnlyStringProperty message = new ReadOnlyStringPropertyBase() {
{
name.addListener(new InvalidationListener() {
// nameプロパティの変化を観察し、変化したらこのプロパティも値が変化したことを通知する
@Override
public void invalidated(Observable o) {
fireValueChangedEvent();
}
});
}
@Override
public String get() {
String value = Person.this.getName();
if (value.length() > 0) {
return "Hello I'm " + value + ".";
} else {
return "";
}
}
@Override
public Object getBean() {
return Person.this;
}
@Override
public String getName() {
return "message";
}
};
// 外部に公開するプロパティ
public ReadOnlyStringProperty messageProperty() {
return message;
}
// JavaBeans仕様と互換を持たせるためのgetter
public final String getMessage() {
return message.get();
}
}
package aoetk.bindingsample.model;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
/**
* Personのテストコード
* @author aoetk
*/
public class PersonTest {
private Person person;
@Before
public void setUp() {
person = new Person();
}
@Test
public void noName() {
person.setName("");
assertThat(person.getMessage(), is(""));
assertThat(person.messageProperty().get(), is(""));
}
@Test
public void oneWordName() {
person.setName("a");
assertThat(person.getMessage(), is("Hello I'm a."));
assertThat(person.messageProperty().get(), is("Hello I'm a."));
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200.0" prefWidth="320.0" xmlns:fx="http://javafx.com/fxml" fx:controller="aoetk.bindingsample.SampleController">
<children>
<Label layoutX="35.0" layoutY="31.0" text="Your name" />
<TextField fx:id="txtName" layoutX="129.0" layoutY="28.0" prefWidth="177.0" />
<Label fx:id="name" layoutX="35.0" layoutY="133.0" />
<Label fx:id="message" layoutX="129.0" layoutY="133.0" />
</children>
</AnchorPane>
package aoetk.bindingsample;
import java.net.URL;
import java.util.ResourceBundle;
import aoetk.bindingsample.model.Person;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
/**
* コントローラの例。
* @author aoetk
*/
public class SampleController implements Initializable {
@FXML
private TextField txtName;
@FXML
private Label name;
@FXML
private Label message;
private Person person;
// 初期化時に必要なバインドを行う
@Override
public void initialize(URL url, ResourceBundle rb) {
person = new Person();
person.nameProperty().bind(txtName.textProperty());
name.textProperty().bind(person.nameProperty());
message.textProperty().bind(person.messageProperty());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment