Hey Guys,
So I have a JTextfield in the cells of 1 of the columns in my JTable
I want to allow only numbers and up to one dot/period. Doesant need to contain a period.
The persiod can be anywhere except at the end of the number.
1 ok
.1 ok
1.1 ok
1.1.1 not ok
- not ok
This is my expression in my code
Currantly it allows only numbers which is good,
but it doesant allow any periods, :(...
private JTextField createTextField() {
JTextField field = new JTextField();
((AbstractDocument) field.getDocument()).setDocumentFilter(new DocumentFilter() {
@Override
public void insertString(FilterBypass fb, int off, String str, AttributeSet attr)
throws BadLocationException {
fb.insertString(off, str.replaceAll("(?<=^| )\\D+(\\.\\D+)?(?=$| )|(?<=^| )\\.\\D+(?=$| )", ""), attr); // remove non-digits
}
@Override
public void replace(FilterBypass fb, int off, int len, String str, AttributeSet attr)
throws BadLocationException {
fb.replace(off, len, str.replaceAll("(?<=^| )\\D+(\\.\\D+)?(?=$| )|(?<=^| )\\.\\D+(?=$| )", ""), attr); // remove non-digits
}
});
return field;
}
Any Help will be greatly appreciated (Y) :)
Thanks guys,