I look all over the net and all the things I tried didn't work....
I found that I can use some JTextFormatterField but it didn't work.
Then I found that I can use DocumentFilter with regex and that what I did:
JTextField jFormattedTextFieldMoneyToConvert = new JTextField();
((AbstractDocument) jFormattedTextFieldMoneyToConvert.getDocument()).setDocumentFilter(new DocumentFilter(){
Pattern regEx = Pattern.compile("^\\d+\\.?\\d*$");
@Override
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
Matcher matcher = regEx.matcher(text);
if (!matcher.matches()) {
return;
}
super.replace(fb, offset, length, text, attrs);
}
});
but it doesn't work...It accepts only digits. I want it to accept also dots. And I also need it to not to start with a dot and not to end with a dot.
what I'm doing wrong?
Copyright Notice:Content Author:「Alex K」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30167524/how-in-java-i-can-create-a-field-that-can-accept-only-digits-and-once-dot