When we work with string-type data on the interface, we generally need to do the following basic controls:
First, provide default values;
Second, confirm whether can be empty;
Third, limit the maximum input length (support Chinese judgment);
The TextField provided by JavaFX does not provide the functionality described above, so we need to extend it.
Stringfield class:
Package Com.lirong.javafx.demo.j3002;import Javafx.beans.property.integerproperty;import Javafx.beans.property.simpleintegerproperty;import javafx.scene.control.textfield;/** * <p>title:lirong Java Application platform</p> * Description: <br> * copyright:corprights lrjap.com<br> * Company:lrJAP.com <br> * * @author YUJJ * @version 1.1.1 * @date 2018-04-29 * @since 9.0.4 */public class Stringfield extends Textfiel d {private Integerproperty maxLength = new Simpleintegerproperty (); Public Stringfield () {This ( -1, null); Public Stringfield (Final Integer maxLength) {This (maxLength, null); } public Stringfield (Integer maxLength, final String DefaultValue) {super (); Limit input Maximum length textproperty (). AddListener ((Observablevalue, OldValue, NewValue), {if (!checkvalueleng Thvalid (NewValue)) {setText (oldValue); } }); Set maximum allowable length if (maxLength = = null) {maxLength =-1; } setmaxlength (MaxLength); if (Isnotblank (defaultvalue)) {//Set default value SetText (defaultvalue); }} protected Boolean Checkvaluelengthvalid (final String value) {if (Getmaxlength ()! = null && GETMA Xlength () > 0 && isnotblank (getText ()) && lenofchinesstring (value) > Getmaxlength ()) {RE Turn boolean.false; } return boolean.true; } public Integer Getmaxlength () {return maxlength.get (); } public Integerproperty Maxlengthproperty () {return maxLength; } public void Setmaxlength (Integer maxLength) {this.maxLength.set (maxLength); }//recommends using the StringUtils class public static Boolean Isnotblank (Final String str) {return (ST) provided in Apache Commons-lang3 R! = NULL &&! "". Equalsignorecase (str)); }//should be used as a tool method public static int lenofchinesstring (final String str) {int len = 0; for (int i = 0; I < str.length (); i++) {char c = str.charat (i); if (c >= ' \u4e00 ' && C <= ' \u9fa5 ') {len + 2; } else {len++; }} return len; }}
Test class:
Package Com.lirong.javafx.demo.j3002;import Javafx.application.application;import Javafx.geometry.hpos;import Javafx.geometry.insets;import Javafx.scene.scene;import Javafx.scene.control.label;import Javafx.scene.layout.columnconstraints;import Javafx.scene.layout.gridpane;import javafx.stage.Stage;/** * <p >title:lirong Java Application platform</p> * Description: <br> * copyright:corprights LRJAP.COM<BR&G T * company:lrjap.com<br> * * @author YUJJ * @version 1.1.1 * @date 2018-04-29 * @since 9.0.4 */public class Teststri Ngfield extends application {public static void main (string[] args) {launch (args); } @Override public void start (Stage primarystage) throws Exception {Gridpane Gridpane = new Gridpane (); Gridpane.setpadding (New Insets (10)); Gridpane.setvgap (10); Gridpane.sethgap (10); columnconstraints col1 = new Columnconstraints (); Col1.setpercentwidth (40); Columnconstraints col2 = new Columnconstraints (); Col2.setpercentwidth (60); Gridpane.getcolumnconstraints (). AddAll (col1, col2); Place two Stringfield label lblcode in gridpane = new label ("Code:"); Stringfield Stringcode = new Stringfield (); Set the maximum input length stringcode.setmaxlength (8); Label Lblname = new Label ("Name:"); Set the maximum input length and the default value Stringfield Stringname = new Stringfield ("Demoname"); Label right-aligned gridpane.sethalignment (Lblcode, hpos.right); Gridpane.sethalignment (Lblname, hpos.right); Gridpane.addrow (0, Lblcode, Stringcode); Gridpane.addrow (1, Lblname, Stringname); Scene scene = new Scene (Gridpane, 400, 300); Primarystage.setscene (Scene); Primarystage.show (); }}
Operating effect:
J3002. JavaFX Component Extensions (ii)--stringfield