JavaFX Properties
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
public class MainController implements Initializable {
final MyNumber myNum = new MyNumber();
@FXML
private Label lblStatus;
@Override
public void initialize(URL location, ResourceBundle resources) {
myNum.setNumber(0);
myNum.numberProperty().addListener(new ChangeListener<Object>() {
@Override
public void changed(ObservableValue<? extends Object> observable,
Object oldValue, Object newValue) {
lblStatus.setText(new Double(myNum.getNumber()).toString());
}
});
}
public void BtnClick(ActionEvent event) {
myNum.setNumber(myNum.getNumber() + 1);
}
}
package application;
import javafx.beans.property.*;
public class MyNumber {
private DoubleProperty number;
public final double getNumber() {
if (number != null)
return number.get();
return 0;
}
public final void setNumber(double number) {
this.numberProperty().set(number);
}
public final DoubleProperty numberProperty() {
if (number == null) {
number = new SimpleDoubleProperty(0);
}
return number;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxWidth="-Infinity" prefHeight="300.0" prefWidth="300.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.40" fx:controller="application.MainController">
<children>
<Button layoutX="117.0" layoutY="171.0" mnemonicParsing="false" onAction="#BtnClick" prefHeight="63.0" prefWidth="104.0" text="Button" />
<Label fx:id="lblStatus" layoutX="117.0" layoutY="72.0" prefHeight="52.0" prefWidth="104.0" text="Label" />
</children>
</AnchorPane>