java - Data validation with JOptionPane -
i want perform data validation while joptionpane.i found following approach not satisfied it
import javax.swing.*; import java.util.regex.*; public class validatejoptionpane { public static void main(string[] args) { string input = joptionpane.showinputdialog("enter number: "); pattern p = pattern.compile("[a-z,a-z,&%$#@!()*^]"); matcher m = p.matcher(input); if (m.find()) { joptionpane.showmessagedialog(null, "please enter numbers"); } } }
it have been better , more sensible use regex detect characters can entered rather testing characters can't entered.
is there better , simpler way data validation joptionpane ? . feel regex overkill here.correct me if wrong:p
p.s beginner java
the long answer is, use documentfilter
, see implementing document filter , documentfilter examples more details.
the problem is, need take control, can't rely on "simple", "helper" functionality provided joptionpane.showinputdialog
, need access text field been used prompt user...
for example...
the following examples uses (slightly) modified version of patternfilter
documentfilter examples
import java.util.regex.matcher; import java.util.regex.pattern; import javax.swing.jcomponent; import javax.swing.jlabel; import javax.swing.joptionpane; import javax.swing.jpanel; import javax.swing.jtextfield; import javax.swing.text.abstractdocument; import javax.swing.text.attributeset; import javax.swing.text.badlocationexception; import javax.swing.text.documentfilter; import javax.swing.text.documentfilter.filterbypass; public class validatetest { public static void main(string[] args) { jtextfield field = new jtextfield(20); pattern p = pattern.compile("[0-9]+"); ((abstractdocument) field.getdocument()).setdocumentfilter(new patternfilter(p)); int option = ask("enter number:", field); if (option == joptionpane.ok_option) { system.out.println("you have entered " + field.gettext()); } } public static int ask(string label, jcomponent comp) { jpanel panel = new jpanel(); panel.add(new jlabel(label)); panel.add(comp); return joptionpane.showoptiondialog(null, panel, label, joptionpane.ok_cancel_option, joptionpane.question_message, null, null, null); } public static class patternfilter extends documentfilter { // useful every kind of input validation ! // insert pattern // pattern must contain subpatterns can enter characters text component ! private pattern pattern; public patternfilter(pattern p) { pattern = p; } public void insertstring(filterbypass fb, int offset, string string, attributeset attr) throws badlocationexception { string newstr = fb.getdocument().gettext(0, fb.getdocument().getlength()) + string; matcher m = pattern.matcher(newstr); if (m.matches()) { super.insertstring(fb, offset, string, attr); } else { } } public void replace(filterbypass fb, int offset, int length, string string, attributeset attr) throws badlocationexception { if (length > 0) { fb.remove(offset, length); } insertstring(fb, offset, string, attr); } } }
now, little clever design, write simple helper class built internally , provided nice askfor(string label, pattern p)
style method return string
(or null
if user canceled operation)
Comments
Post a Comment