I need to adjust the data in some TextField directly in the input process. What exactly am I trying to achieve:
- Input only numbers strictly
- Numbers must be [1; 500], and only in this gap
- When deleting all characters, 1-tsa is automatically recorded in the field, and if the number 500 is exceeded, 500
- Leading zeros are automatically truncated (you can not enter a zero, or an example with the number "105", delete the unit - the text is corrected to just "5").
To do this, I hung the handler on TextField and wrote the following code into it:
valueSliderStrokeWidth.textProperty().addListener(new ChangeListener<String>() { @Override public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) { System.out.println("edit text"); String text = newValue; if(text.equals("")){ valueSliderStrokeWidth.setText("1"); return; } int num; try{ num = Integer.parseInt(text); if(num == 0){ valueSliderStrokeWidth.setText("1"); }else if(num > 500){ valueSliderStrokeWidth.setText("500"); }else{ valueSliderStrokeWidth.setText(String.valueOf(num)); } }catch(Exception ex){ valueSliderStrokeWidth.setText(oldValue); } } }); Everything works correctly, except for one, namely, the removal of leading zeros in the input process. For example - an example with the number "105". By removing the unit, I get the following trace:
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: The start must be <= the end at javafx.scene.control.TextInputControl.getText (TextInputControl.java 12) at javafx.scene.control.TextInputCont. TextInputControl.java Zenu64) at javafx.scene.control.TextInputControl.refxText (TextInputControl.java.548) at javafx.scene.control. (TextInputControl.java:899) at com.sun.javafx.scene.control. Skin. : 198) at com.sun.javafx.scene.control.e. com.sun.javafx.scene.control.behavior.BehaviorBase.callActionFor Event (BehaviorBase.java:218) at com.sun.javafx.scene.control. (BehaviorBase.java:135) at com.sun.javafx.event.CompositeEventHandler $ NormalEventHandlerRecord.handleBubblingEvent (CompositeEventHandler.java:218) at com.sun.javafx.event.CompositeEventHandler.dispartyffy of our hp, 2010 by the origin of our team of our hp, in the help of our home account most of them, in the index of our team ,.javafen.javafx.event. sun.javafx.event.EventHandlerManager.dispatchBubblingEvent (EventHandlerManager.java:238) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEd.js online secured browsing your business apartament.com | java: 59) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent (BasicEventDispatcher.java:58) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent (EventDispatchChainImpl.java:114) at com.sun.javafx.event .BasicEventDispatcher.dispatchEvent (BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent (EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent in isethEehetheEhristn.eventEefehEjep.Event.javafx.event.BasicEvent. javafx.event.EventDispatchChainImpl. 54) at javafx.event.Event.fireEvent (Event.java:198) at javafx.scene.Scene $ KeyHandler.process (Scene.java:3964) at javafx.scene.Scene $ KeyHandler.access $ 1800 (Scene.java: 3910) at javafx.scene.Scene.impl_processKeyEvent (Scene.java:2040) at javafx.scene.Scene $ ScenePeerListener.keyEvent (Scene.java:2501) at com.sun.javafx.tk.quantum.GlassViewEventAlindrndeactand. (GlassViewEventHandler.java:217) at com.sun.javafx.tk.quantum.GlassViewEventHandler $ KeyEventNotification.run (GlassViewEventHandler.java:149) at java.security.AccessController.doPrivileged (Nativ e Method) at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda $ handleKeyEvent $ 352 (GlassViewEventHandler.java:248) at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock (QuantumoTo.Tool.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock (QuantumoTo.Tv.um.QuantumToolkit.runWandoutRenderLock) $ 452 (e-mail.extra.javafx.tk.quantum.GlassViewEventHandler.lambda $ handleKeyEvent $ 352) .sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent (GlassViewEventHandler.java:247) at com.sun.glass.ui.View.handleKeyEvent (View.java vector46) at com.sun.glass.ui.View.notifyKey (View.java:966) at com.sun.glass.ui.win.WinApplication._runLoop (Native Method) at com.sun.glass.ui.win.WinApplication.lambda $ null $ 147 (WinApplication.java:177) at java.lang.Thread.run (Thread.java:748)
Tell me what and where I'm doing wrong, or a more elegant solution. Thank.