1 /* 2 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package ensemble.samples.controls.text.textformatter; 27 28 import javafx.application.Application; 29 import javafx.beans.property.DoubleProperty; 30 import javafx.beans.property.SimpleDoubleProperty; 31 import javafx.scene.Parent; 32 import javafx.scene.Scene; 33 import javafx.scene.control.TextField; 34 import javafx.scene.control.TextFormatter; 35 import javafx.stage.Stage; 36 import javafx.util.converter.FormatStringConverter; 37 38 import java.text.NumberFormat; 39 40 /** 41 * Demonstrates a TextField control with a TextFormatter that filters and formats the content. 42 * 43 * @sampleName Text Formatter 44 * @preview preview.png 45 * @docUrl http://docs.oracle.com/javase/8/javafx/user-interface-tutorial/text.htm#JFXUI734 Using JavaFX Text 46 * @see javafx.scene.control.TextFormatter 47 * @see javafx.scene.control.TextField 48 * @see javafx.scene.control.TextInputControl 49 * @see javafx.util.converter.FormatStringConverter 50 * @playground price (min=0, max=10000) 51 * @embedded 52 * 53 * @related /Controls/Text/Advanced Label 54 * @related /Controls/Text/Bidi 55 * @related /Controls/Text/Inset Text 56 * @related /Controls/Button/Graphic Button 57 * @related /Controls/Text/Search Box 58 * @related /Controls/Text/Simple Label 59 * @related /Controls/Text/Text Field 60 * @related /Controls/Text/TextFlow 61 * @related /Controls/Text/Text Validator 62 */ 63 public class TextFormatterApp extends Application{ 64 65 private DoubleProperty price = new SimpleDoubleProperty(1200.555); 66 67 public Parent createContent() { 68 final NumberFormat currencyInstance = NumberFormat.getCurrencyInstance(); 69 String symbol = currencyInstance.getCurrency().getSymbol(); 70 FormatStringConverter<Number> converter = 71 new FormatStringConverter<>(currencyInstance); 72 TextFormatter<Number> formatter = new TextFormatter<>(converter); 73 formatter.valueProperty().bindBidirectional(price); 74 final TextField text = new TextField(); 75 text.setTextFormatter(formatter); 76 text.setMaxSize(140, TextField.USE_COMPUTED_SIZE); 77 return text; 78 } 79 80 @Override 81 public void start(Stage primaryStage) throws Exception { 82 primaryStage.setScene(new Scene(createContent())); 83 primaryStage.show(); 84 } 85 86 /** 87 * Java main for when running without JavaFX launcher 88 * @param args command line arguments 89 */ 90 public static void main(String[] args) { 91 launch(args); 92 } 93 }