java - Validating User Input using JavaFX and FXML -


using javafx , fxml want validate users' input when hit submit. looking @ .fxml doc, you'll see have 2 input fields 1 zip code , 1 cityname. enforcing numbertextfield.java , citynametextfield.java xml file, how can make sure not blank when user hit submit?

fxmldocument.fxml

<?xml version="1.0" encoding="utf-8"?>  <?import java.lang.*?> <?import java.util.*?> <?import javafx.scene.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <?import validatetextfieldsfxml.custom.*?>  <pane maxheight="-infinity" maxwidth="-infinity" minheight="-infinity" minwidth="-infinity" prefheight="400.0" prefwidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">    <children>       <label layoutx="173.0" layouty="89.0" text="zipcode" />       <label layoutx="173.0" layouty="118.0" text="cityname" />       <numbertextfield fx:id="zipcodetf" layoutx="257.0" layouty="84.0" />       <citynametextfield fx:id="citynametf" layoutx="257.0" layouty="113.0" />       <button fx:id="btn" layoutx="217.0" layouty="162.0" mnemonicparsing="false" text="submit" />    </children> </pane> 

citynametextfield.java

package validatetextfieldsfxml.custom;  import javafx.scene.control.textfield; public class citynametextfield extends textfield {     public citynametextfield(){        this.setprompttext("enter cityname");    }     @override    public void replacetext(int i, int il, string string){        if(string.matches("[a-za-z]") || string.isempty()){           super.replacetext(il, il, string);        }    }      @override     public void replaceselection(string string) {         super.replaceselection(string);      }    } 

numbertextfield.java

package validatetextfieldsfxml.custom; import javafx.scene.control.textfield;  public class numbertextfield extends textfield{     public numbertextfield(){         this.setprompttext("enter numbers");     }     @override     public void replacetext(int i, int il, string string){         if(string.matches("[0-9]") || string.isempty() ){             super.replacetext(i, il, string);         }     }      @override     public void replaceselection(string string){         super.replaceselection(string);     } } 

in controller's initialize method, do

btn.disableproperty().bind(zipcodetf.textproperty().isempty()     .or(citynametf.textproperty().isempty())); 

you can directly in fxml file with

  <button fx:id="btn" text="submit"         disable="${ citynametf.text.empty || zipcodetf.text.empty }"/> 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -