Skip to content

Instantly share code, notes, and snippets.

@aoetk
Created July 12, 2015 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aoetk/9c74ee1bb17afb304bff to your computer and use it in GitHub Desktop.
Save aoetk/9c74ee1bb17afb304bff to your computer and use it in GitHub Desktop.
http://aoe-tk.hatenablog.com/entry/2015/07/12/173402 内のサンプルの全コード。
package sample;
import javafx.beans.binding.IntegerBinding;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.Spinner;
import javafx.scene.control.SpinnerValueFactory;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
Label answer;
@FXML
Spinner<Integer> valueA;
@FXML
Spinner<Integer> valueB;
@Override
public void initialize(URL location, ResourceBundle resources) {
SpinnerValueFactory.IntegerSpinnerValueFactory vfA =
new SpinnerValueFactory.IntegerSpinnerValueFactory(0, 100, 1);
valueA.setValueFactory(vfA);
SpinnerValueFactory.IntegerSpinnerValueFactory vfB =
new SpinnerValueFactory.IntegerSpinnerValueFactory(0, 100, 2);
valueB.setValueFactory(vfB);
bind();
}
private void bind() {
IntegerProperty a = new SimpleIntegerProperty(0);
a.bind(valueA.valueProperty());
IntegerBinding a1 = a.add(1); // a1 = a + 1
IntegerProperty b = new SimpleIntegerProperty(0);
b.bind(valueB.valueProperty());
IntegerBinding b1 = b.add(-1).multiply(2); // b1 = (b - 1) * 2
answer.textProperty().bind(a1.add(b1).asString()); // a1 + b1
}
}
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class ReactiveSampleApp extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Reactive Sample");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="119.0" prefWidth="424.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Spinner fx:id="valueA" layoutX="39.0" layoutY="43.0" prefHeight="26.0" prefWidth="82.0" />
<Spinner fx:id="valueB" layoutX="178.0" layoutY="43.0" prefHeight="26.0" prefWidth="82.0" />
<Label layoutX="127.0" layoutY="48.0" text="+ 1) + (" />
<Label layoutX="25.0" layoutY="48.0" text="(" />
<Label layoutX="270.0" layoutY="48.0" text="- 1) * 2 =" />
<Label fx:id="answer" layoutX="330.0" layoutY="48.0" />
</children>
</Pane>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment