I have modified NumberFormatter to have like a currency instance (with prefix).
When I write the first number, this application have add the prefix to the number
JFormattedTextField Empty
but when I do that, the caret position change before the first number like this
enter image description here
How can I fix this by only modifying method formato() – that returns a NumberFormatter – to the constructor of JFormattedTextField?
textFieldMonto = new javax.swing.JFormattedTextField(formato());
This is the method:
private NumberFormatter formato() {
DecimalFormat myFormatter = new DecimalFormat("'Gs. '###,##0;'Gs. '###,##0");
NumberFormatter numberFormatter = new NumberFormatter(myFormatter) {
// this change caret to the end in every focus gained
@Override
public void install(JFormattedTextField pField) {
super.install(pField);
pField.setCaretPosition(pField.getDocument().getLength());
}
// allow empty text on JFormattedTextField and dont allow negative numbers
@Override
public String valueToString(Object value) throws ParseException {
String result = super.valueToString(value);
if(super.valueToString(value).startsWith("-"))
result = result.replaceFirst("-", ""); // this block every negative number
if(value==null)
return "";
return result;
}
// allow empty text on JFormattedTextField and dont allow negative numbers
@Override
public Object stringToValue(String text) throws ParseException {
if (text.length() == 0 || text.equals("Gs. ")) // if is empty or only have the prefix, return null
return null;
text.replaceFirst("-", ""); // this block every negative number
if(!text.startsWith("Gs. ")) //if is empty, add the prefix "Gs. " to the number
text = "Gs. " + text;
return super.stringToValue(text);
}
};
numberFormatter.setAllowsInvalid(false); //this is the key!!
numberFormatter.setMaximum(new BigDecimal("999999999999"));// maximum number to put
numberFormatter.setCommitsOnValidEdit(true);// commit value on each keystroke instead of focus lost
return numberFormatter;
}
I don't think this comment, in the code in your question, is true:
Method
installis not called every time theJFormattedTextFieldgains focus. According to my tests, it is called only when aJFormattedTextFieldobject is created.According to the javadoc:
Hence I suggest adding a DocumentListener to the
JFormattedTextField's document, in methodinstall.Note that there are other
DocumentListeners that are set up by the Swing infrastructure and since there is no guarantee regarding the order of execution when there is more than oneDocumentListener, the implementation, in the below code, uses invokeLater to ensure that the listener that I added is run last.Here is my rewrite of your
formatomethod.The only thing I changed was method
install.