java - Validating text fields completeness with logical conditions -
i'm coding javafx app, have 6 textfields
required fields. have default button innactive @ first.
i want use logic check wether fields have been filled. have variable estado
initiated false
.
coded:
if (!((textfield)event.getsource()).gettext().trim().isempty()) estado = estado | true; else estado = false; if (estado) button.setdisabled(false)
all textfields
got method after keyreleased
event.
i've got not results fo far.
this basic solution if need more plz tell...
/* * change template, choose tools | templates * , open template in editor. */ package textfieldlogic; import java.util.regex.matcher; import java.util.regex.pattern; import javafx.application.application; import javafx.event.eventhandler; import javafx.scene.scene; import javafx.scene.control.textfield; import javafx.scene.input.keyevent; import javafx.scene.layout.stackpane; import javafx.scene.layout.vbox; import javafx.stage.stage; /** * * @author reegan */ public class textfieldlogic extends application { @override public void start(stage primarystage) { formattedtextfield btn = new formattedtextfield("^[1-9]+$"); btn.settext("only enter numbers"); formattedtextfield btn1 = new formattedtextfield("^[a-z]+$"); btn1.settext("only enter characters"); vbox vbox = new vbox(20); vbox.getchildren().addall(btn,btn1); stackpane root = new stackpane(); root.getchildren().add(vbox); scene scene = new scene(root, 300, 250); primarystage.settitle("hello world!"); primarystage.setscene(scene); primarystage.show(); } /** * main() method ignored in correctly deployed javafx application. * main() serves fallback in case application can not * launched through deployment artifacts, e.g., in ides limited fx * support. netbeans ignores main(). * * @param args command line arguments */ public static void main(string[] args) { launch(args); } public class formattedtextfield extends textfield { matcher matcher; public formattedtextfield(string regex) { pattern pattern = pattern.compile(regex); matcher = pattern.matcher(""); eventhandler<keyevent> filter = new eventhandler<keyevent>() { @override public void handle(keyevent t) { string s = gettext() + t.getcharacter(); matcher.reset(s); if (!( matcher.matches() || matcher.hitend())) { t.consume(); } } }; addeventfilter(keyevent.key_typed, filter); } } }
Comments
Post a Comment