public void insertString(int offset, String str, AttributeSet attr) { try { int integerSizeOfField = adaptee.size_ - adaptee.decimal_; if (adaptee.decimal_ > 0 && str.length() == 1) { String wrkStr0 = super.getText(0, super.getLength()); wrkStr0 = wrkStr0.substring(0, offset) + str + wrkStr0.substring(offset, wrkStr0.length()); String wrkStr1 = wrkStr0.replace(".", ""); wrkStr1 = wrkStr1.replace(",", ""); wrkStr1 = wrkStr1.replace("-", ""); if (wrkStr1.length() > adaptee.size_) { wrkStr1 = wrkStr1.substring(0, integerSizeOfField) + "." + wrkStr1.substring(integerSizeOfField, wrkStr1.length() - 1); super.replace(0, super.getLength(), wrkStr1, attr); } else { int posOfDecimal = wrkStr0.indexOf("."); if (posOfDecimal == -1) { if (wrkStr1.length() > integerSizeOfField) { wrkStr1 = wrkStr1.substring(0, integerSizeOfField) + "." + wrkStr1.substring(integerSizeOfField, wrkStr1.length()); super.replace(0, super.getLength(), wrkStr1, attr); } else { super.insertString(offset, str, attr); } } else { int decimalLengthOfInputData = wrkStr0.length() - posOfDecimal - 1; if (decimalLengthOfInputData <= adaptee.decimal_) { super.insertString(offset, str, attr); } } } } else { if (str.contains(".")) { JOptionPane.showMessageDialog(null, XFUtility.RESOURCE.getString("NumberFormatError")); } else { String wrkStr0 = super.getText(0, super.getLength()); wrkStr0 = wrkStr0.substring(0, offset) + str + wrkStr0.substring(offset, wrkStr0.length()); String wrkStr1 = wrkStr0.replace(".", ""); wrkStr1 = wrkStr1.replace(",", ""); wrkStr1 = wrkStr1.replace("-", ""); if (wrkStr1.length() <= adaptee.size_) { super.insertString(offset, str, attr); } } } } catch (BadLocationException e) { e.printStackTrace(); } }
public final void setNameText(final String name) { try { myNameDocument.replace(0, myNameDocument.getLength(), name, null); } catch (BadLocationException e) { LOG.error(e); } }
/** * Overrides PlainDocument.replace, to suppress separate change operations on delete and insert. * This avoids temporarily seeing an invalid field when setText is called, thus possibly losing * a button press. */ public void replace(int offset, int length, String text, AttributeSet attrs) throws BadLocationException { isReplacing = true; String oldText = LTextField.this.getText(); super.replace(offset, length, text, attrs); isReplacing = false; setChanged(true); firePropertyChange("text", oldText, LTextField.super.getText()); }
@Override public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException { if (str == null) return; int oldLength = super.getLength(); int insertLength = str.length(); if (oldLength + insertLength <= maxLength) { super.insertString(offset, str, attr); } else { str = getNewContent(oldLength, insertLength, str); super.replace(0, oldLength, str, attr); } }