java - jtextfield documentFilter once defined, value not loading in the jtextfield -
i have jtextfield named tpvv, wrote documentfilter accept numbers, maximum length of 3.and have button edit, when click button entire row loaded in textfield's editing jtable(value in jtextfield tpvv remains constant). jtextfield defined without documentfilter works well(loads values jtable textfields based on row selection). when comment documentfilter works well, cannot provide validation (accept number , length of 3).
i need check validation tpvv , load values jtable based on different row selection clicking edit button.
`class numericandlengthfilter extends documentfilter { /** * number of characters allowed. */ private int length = 0; /** * restricts number of charcacters can entered given length. * @param length number of characters allowed. */ public numericandlengthfilter(int length) { this.length = length; } @override public void insertstring(filterbypass fb, int offset, string string, attributeset attr) throws badlocationexception { if (isnumeric(string)) { if (this.length > 0 && fb.getdocument().getlength() + string. length() > this.length) { return; } super.insertstring(fb, offset, string, attr); } } @override public void replace(filterbypass fb, int offset, int length, string text, attributeset attrs) throws badlocationexception { if (isnumeric(text)) { if (this.length > 0 && fb.getdocument().getlength() + text. length() > this.length) { return; } super.insertstring(fb, offset, text, attrs); } } /** * method tests whether given text can represented number. * method can enhanced further specific needs. * @param text input text. * @return {@code true} if given string can converted number; otherwise returns {@code false}. */ private boolean isnumeric(string text) { if (text == null || text.trim().equals("")) { return false; } (int icount = 0; icount < text.length(); icount++) { if (!character.isdigit(text.charat(icount))) { return false; } } return true; } } //((abstractdocument) tpvv.getdocument()).setdocumentfilter(new numericandlengthfilter(3));
`last commented line defined in code validation purpose call. please solve problem.
you're ignoring parameters passed , mean...
offset
offset within document newtext
inserted...length
number of characters deleted
now, if use length
within if
statement, start see difference. basically, when call settext
, length
equal number of characters in text field (as text replaced)...
public void replace(filterbypass fb, int offset, int length, string text, attributeset attrs) throws badlocationexception { if (isnumeric(text)) { system.out.println(offset + "; " + length + "; " + text); if (this.length > 0 && fb.getdocument().getlength() + text.length() - length > this.length) { return; } super.replace(fb, offset, length, text, attrs); } }
you're calling super.insertstring(fb, offset, text, attrs);
in replace
method, isn't helpful either...
update comments
the problem you're facing settext(null)
fact isnumeric
in filter returning false
null
, empty ("") values, these valid in contexts...
now, alter isnumeric
method, might adversely affect other methods of filter, instead, should place additional condition in replace
catch case , deal appropriately...
public void replace(filterbypass fb, int offset, int length, string text, attributeset attrs) throws badlocationexception { if (isnumeric(text)) { system.out.println(offset + "; " + length + "; " + text); if (this.length > 0 && fb.getdocument().getlength() + text.length() - length > this.length) { return; } super.replace(fb, offset, length, text, attrs); } else if (text == null || text.equals("")) { super.replace(fb, offset, length, text, attrs); } }
Comments
Post a Comment