java - Alphabets validation of jtextfield -



java - Alphabets validation of jtextfield -

i have been trying display alphabets in jtextfield.even though other keys pressed jtextfield should not display them alphabets displayed.can please help me this..

start taking @ implementing document filter

import java.awt.eventqueue; import javax.swing.jframe; import javax.swing.jtextfield; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.text.abstractdocument; import javax.swing.text.attributeset; import javax.swing.text.badlocationexception; import javax.swing.text.documentfilter; public class testfilter { public static void main(string[] args) { new testfilter(); } public testfilter() { eventqueue.invokelater(new runnable() { @override public void run() { seek { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } grab (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { ex.printstacktrace(); } jtextfield field = new jtextfield(10); ((abstractdocument)field.getdocument()).setdocumentfilter(new charfilter()); jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new gridbaglayout()); frame.add(field); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class charfilter extends documentfilter { @override public void insertstring(documentfilter.filterbypass fb, int offset, string string, attributeset attr) throws badlocationexception { stringbuilder buffer = new stringbuilder(string); (int = buffer.length() - 1; >= 0; i--) { char ch = buffer.charat(i); if (!character.isalphabetic(ch)) { buffer.deletecharat(i); } } super.insertstring(fb, offset, buffer.tostring(), attr); } @override public void replace(documentfilter.filterbypass fb, int offset, int length, string string, attributeset attr) throws badlocationexception { if (length > 0) { fb.remove(offset, length); } insertstring(fb, offset, string, attr); } } }

you ma find documentfilter examples helpful

java swing

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -