@Override public void replace( DocumentFilter.FilterBypass filterBypass, int offset, int length, String text, AttributeSet attributeSet) throws BadLocationException { text = trimHexadecimal(text); Document document = filterBypass.getDocument(); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(document.getText(0, document.getLength())); stringBuilder.insert(offset, text); if (isValidInput(stringBuilder.toString(), length)) { super.replace(filterBypass, offset, length, text, attributeSet); } else { Toolkit.getDefaultToolkit().beep(); } }
@Override public void replace( DocumentFilter.FilterBypass fp, int offset, int length, String string, AttributeSet aset) throws BadLocationException { Document doc = fp.getDocument(); String oldText = doc.getText(0, doc.getLength()); StringBuilder sb = new StringBuilder(oldText); sb.replace(offset, offset + length, oldText); int len = string.length(); boolean isValidInteger = true; for (int i = 0; i < len; i++) { if (!Character.isDigit(string.charAt(i))) { isValidInteger = false; break; } } if (isValidInteger && verifyText(sb.toString())) { super.replace(fp, offset, length, string, aset); } else Toolkit.getDefaultToolkit().beep(); }