I wish to restrict the text entered into a JTextField to just digits and limit the string to 4 chars long. I have tried to do this by attaching a DocumentListener to monitor changes and delete unwanted characters, but this throws an error I presume because I am causing an endless loop by changing the JTextField that the listener is watching while it is still in the process of responding to the previous change.
I don't know how to make this threadsafe. But I have heard of an alternative and possibly better method than attaching a listener to a JTextField which involves creating a subclass of PlainDocument, overriding its insertString method and getting JTextField to use this instead. Has anybody tried this, or have any further details about how to implement this? My experience with java is fairly (and probably apparently) limited. Any help appreciated.
Greg
I don't know how to make this threadsafe. But I have heard of an alternative and possibly better method than attaching a listener to a JTextField which involves creating a subclass of PlainDocument, overriding its insertString method and getting JTextField to use this instead. Has anybody tried this, or have any further details about how to implement this? My experience with java is fairly (and probably apparently) limited. Any help appreciated.
Greg