apps/samples/Ensemble8/src/samples/java/ensemble/samples/language/stringbinding/StringBindingApp.java

Print this page




  43 import javafx.scene.control.Label;
  44 import javafx.scene.control.TextField;
  45 import javafx.scene.layout.VBox;
  46 import javafx.stage.Stage;
  47 
  48 /**
  49  * A sample that demonstrates how to bind text properties so the
  50  * value of the bound property is updated automatically when the value
  51  * of the original property is changed.
  52  *
  53  * @sampleName String Binding
  54  * @preview preview.png
  55  * @see javafx.beans.binding.StringBinding
  56  * @see javafx.scene.control.TextField
  57  * @see javafx.scene.control.Label
  58  * @embedded
  59  */
  60 public class StringBindingApp extends Application {
  61 
  62     public Parent createContent() {
  63         final SimpleDateFormat format = new SimpleDateFormat("mm/dd/yyyy");

  64         final TextField dateField = new TextField();
  65         dateField.setPromptText("Enter a birth date");
  66         dateField.setMaxHeight(TextField.USE_PREF_SIZE);
  67         dateField.setMaxWidth(TextField.USE_PREF_SIZE);
  68 
  69         Label label = new Label();
  70         label.setWrapText(true);
  71         label.textProperty().bind(new StringBinding() {
  72             {
  73                 bind(dateField.textProperty());
  74             }
  75 
  76             @Override
  77             protected String computeValue() {
  78                 try {
  79                     Date date = format.parse(dateField.getText());
  80                     Calendar c = Calendar.getInstance();
  81                     c.setTime(date);
  82 
  83                     Date today = new Date();
  84                     Calendar c2 = Calendar.getInstance();
  85                     c2.setTime(today);
  86 
  87                     if (c.get(Calendar.DAY_OF_YEAR) == c2.get(Calendar.DAY_OF_YEAR) - 1
  88                             && c.get(Calendar.YEAR) == c2.get(Calendar.YEAR)) {
  89                         return "You were born yesterday";



  90                     } else {
  91                         return "You were born " + format.format(date);
  92                     }
  93                 } catch (Exception e) {
  94                     return "Enter a valid birth date (mm/dd/yyyy)";
  95                 }
  96             }
  97         });
  98 
  99         VBox vBox = new VBox(7);
 100         vBox.setPadding(new Insets(12));
 101         vBox.getChildren().addAll(label, dateField);
 102         vBox.setAlignment(Pos.CENTER);
 103         return vBox;
 104     }
 105 
 106     @Override
 107     public void start(Stage primaryStage) throws Exception {
 108         primaryStage.setScene(new Scene(createContent()));
 109         primaryStage.show();
 110     }
 111 
 112     /**
 113      * Java main for when running without JavaFX launcher
 114      * @param args command line arguments


  43 import javafx.scene.control.Label;
  44 import javafx.scene.control.TextField;
  45 import javafx.scene.layout.VBox;
  46 import javafx.stage.Stage;
  47 
  48 /**
  49  * A sample that demonstrates how to bind text properties so the
  50  * value of the bound property is updated automatically when the value
  51  * of the original property is changed.
  52  *
  53  * @sampleName String Binding
  54  * @preview preview.png
  55  * @see javafx.beans.binding.StringBinding
  56  * @see javafx.scene.control.TextField
  57  * @see javafx.scene.control.Label
  58  * @embedded
  59  */
  60 public class StringBindingApp extends Application {
  61 
  62     public Parent createContent() {
  63         final SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
  64         format.setLenient(false);
  65         final TextField dateField = new TextField();
  66         dateField.setPromptText("Enter a birth date");
  67         dateField.setMaxHeight(TextField.USE_PREF_SIZE);
  68         dateField.setMaxWidth(TextField.USE_PREF_SIZE);
  69 
  70         Label label = new Label();
  71         label.setWrapText(true);
  72         label.textProperty().bind(new StringBinding() {
  73             {
  74                 bind(dateField.textProperty());
  75             }
  76 
  77             @Override
  78             protected String computeValue() {
  79                 try {
  80                     Date date = format.parse(dateField.getText());
  81                     Calendar c = Calendar.getInstance();
  82                     c.setTime(date);
  83 
  84                     Date today = new Date();
  85                     Calendar c2 = Calendar.getInstance();
  86                     c2.setTime(today);
  87                     
  88                     if (c.get(Calendar.DAY_OF_YEAR) == c2.get(Calendar.DAY_OF_YEAR) - 1
  89                             && c.get(Calendar.YEAR) == c2.get(Calendar.YEAR)) {
  90                         return "You were born yesterday";
  91                     } else if (c.get(Calendar.DAY_OF_YEAR) == c2.get(Calendar.DAY_OF_YEAR)
  92                                && c.get(Calendar.YEAR) == c2.get(Calendar.YEAR)) {
  93                         return "You were born today";
  94                     } else {
  95                         return "You were born " + format.format(date);
  96                     }
  97                 } catch (Exception e) {
  98                     return "Enter your valid birth date (mm/dd/yyyy)";
  99                 }
 100             }
 101         });
 102 
 103         VBox vBox = new VBox(7);
 104         vBox.setPadding(new Insets(12));
 105         vBox.getChildren().addAll(label, dateField);
 106         vBox.setAlignment(Pos.CENTER);
 107         return vBox;
 108     }
 109 
 110     @Override
 111     public void start(Stage primaryStage) throws Exception {
 112         primaryStage.setScene(new Scene(createContent()));
 113         primaryStage.show();
 114     }
 115 
 116     /**
 117      * Java main for when running without JavaFX launcher
 118      * @param args command line arguments