@Override public void insertString( DocumentFilter.FilterBypass fp, int offset, String string, AttributeSet aset) throws BadLocationException { Document doc = fp.getDocument(); String oldText = doc.getText(0, doc.getLength()); StringBuilder sb = new StringBuilder(oldText); sb.insert(offset, string); 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.insertString(fp, offset, string, aset); else Toolkit.getDefaultToolkit().beep(); }
@Override public void insertString( FilterBypass filterBypass, int offset, 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(), 0)) { super.insertString(filterBypass, offset, text, attributeSet); } else { Toolkit.getDefaultToolkit().beep(); } }