1 /* 2 * Copyright (c) 2008, 2014, Oracle and/or its affiliates. 3 * All rights reserved. Use is subject to license terms. 4 * 5 * This file is available and licensed under the following license: 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * - Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * - Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the distribution. 16 * - Neither the name of Oracle Corporation nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package ensemble.samples.language.stringbinding; 33 34 import java.text.SimpleDateFormat; 35 import java.util.Calendar; 36 import java.util.Date; 37 import javafx.application.Application; 38 import javafx.beans.binding.StringBinding; 39 import javafx.geometry.Insets; 40 import javafx.geometry.Pos; 41 import javafx.scene.Parent; 42 import javafx.scene.Scene; 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 115 */ 116 public static void main(String[] args) { 117 launch(args); 118 } 119 }