Java 8 U40 TextFormatter (JavaFX) to restrict user input to decimal only

I am looking for an example to restrict user input of only numbers and decimal points using the new TextFormatter Java8 u40 class. http://download.java.net/jdk9/jfxdocs/javafx/scene/control/TextFormatter.Change.html

+5
java-8 javafx javafx-8
Jun 25 '15 at 0:46
source share
1 answer

See this example:

 DecimalFormat format = new DecimalFormat( "#.0" ); TextField field = new TextField(); field.setTextFormatter( new TextFormatter<>(c -> { if ( c.getControlNewText().isEmpty() ) { return c; } ParsePosition parsePosition = new ParsePosition( 0 ); Object object = format.parse( c.getControlNewText(), parsePosition ); if ( object == null || parsePosition.getIndex() < c.getControlNewText().length() ) { return null; } else { return c; } })); 
+9
Jun 25 '15 at 6:48
source share



All Articles