Lets say a user has to input a Double value into the Jtextfield which then gets calculated.
But if the user suddenly used more than 1 period it would trigger a NumberFormatException, so i assume the solution would be using a Document Filter to filter out any extra periods or catching the exception and notifying the user of an invalid input
Currenty using a DocumentFilter to only allow digits and periods, but my problem is how to filter out a second period
PlainDocument filter = new PlainDocument();
filter.setDocumentFilter(new DocumentFilter() {
@Override
public void insertString(FilterBypass fb, int off, String str, AttributeSet attr)
throws BadLocationException
{
fb.insertString(off, str.replaceAll("[^0-9.]", ""), attr);
}
@Override
public void replace(FilterBypass fb, int off, int len, String str, AttributeSet attr)
throws BadLocationException
{
fb.replace(off, len, str.replaceAll("[^0-9.]", ""), attr);
}
});
apm.setDocument(filter);
Example
Invalid INPUT: 1.2.2
Valid INPUT: 1.22