I'm working my way through the scjd book (AM & TC).
I want to validate a JTextField as 8 digits. The PositiveInteger class (using extends PlainDocument) example does nearly what I want and I have tweaked a version to stop checking when 8 digits entered.
However, if you back up and remove a few digits and then start adding more in the middle. The starting offset is not what I was hoping it would be, because of this it goes past 8 digits. It appears to be giving the offset starting from the point of new digits being entered and not the start of the field. Is there a work around for this?
public void insertString(int offs, String str, AttributeSet a)
From looking ar previous posts there are many ways to do this but the one I will use will be one of the following 3:
1 MaskFormatter will functionally provide what I am looking for and is the easiest to implement. But it's cheap and cheerful.
2 extends PlainDocument : I like the feel of the way the PositiveInteger works. Should I keep tweaking until I nail down the offset or will it not do what I am looking for?
3 DocumentListener: have'nt fully explored yet.
I also doubted between all the alternatives to block invalid input, started a thread in the swing-forum here. So used the DocumentFilter.
For the assignment I created 2 filters: one allowing a maximum number of chars, the other one allowing a maximum number of digits. The last one I used for the customer id.