Friday, 15 March 2013

java - IDocumentFilter using a HashMap as a dictionary Menu , to validate on a JTextField -



java - IDocumentFilter using a HashMap as a dictionary Menu , to validate on a JTextField -

i have hashmap filled info table in sybase , structure

-index - characters

----1---- 1234567890

----2----- abcdefg..

i'm tryng set new customjtextfield extended jtextfield, command have atribute named mask , can set in attribute mask number ,something this:

customtextfield = new customtextfield(20); customtextfield.set_mask(1);

i have customtextfield working attributes , behaviour of mask atribute not allow user write letter not contained in table , if customtextfield set mask(1) , user able write numbers

i need help using documentfilter, or suggestion , need dictionaries table database,(user requirement),

edit*

by recommendation im trying illustration of documentfilter allow utilize type characters contained in array ( createed hashmap)

you may wish utilize document filter instead.

this allow build filters restrict user type, rather relying on post validation.

check here examples

updated

this surprisingly simple. using examples linked. i'm sure you'll able adapt them needs.

public class testdocumentfilter01 { public static void main(string[] args) { new testdocumentfilter01(); } public testdocumentfilter01() { eventqueue.invokelater(new runnable() { @override public void run() { seek { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } grab (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { } jframe frame = new jframe("test"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new borderlayout()); frame.add(new testpane()); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class testpane extends jpanel { public testpane() { setlayout(new gridbaglayout()); gridbagconstraints gbc = new gridbagconstraints(); gbc.gridwidth = gridbagconstraints.remainder; add(createfield("1234567890"), gbc); add(createfield("stackoverflow"), gbc); add(createfield("abcdefghijklmnopqrstuvwxyz "), gbc); } protected jtextfield createfield(string mask) { jtextfield field = new jtextfield(10); maskfilter df = new maskfilter(); df.setmask(mask); ((abstractdocument) (field.getdocument())).setdocumentfilter(df); homecoming field; } } public class maskfilter extends documentfilter { private char[] maskset; private string mask; public void setmask(string mask) { this.mask = mask; this.maskset = mask.tochararray(); arrays.sort(this.maskset); } public string getmask() { homecoming mask; } public void insertstring(documentfilter.filterbypass fb, int offset, string string, attributeset attr) throws badlocationexception { stringbuffer buffer = new stringbuffer(string); (int = buffer.length() - 1; >= 0; i--) { char ch = buffer.charat(i); if (arrays.binarysearch(maskset, ch) < 0) { buffer.deletecharat(i); } } super.insertstring(fb, offset, buffer.tostring(), attr); } 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); } } }

java swing validation hashmap jtextfield

No comments:

Post a Comment