Max
Max

Reputation: 269

Limit User Control of JTextField

I've got a game where you create words using letters in a letter bank. Every time a letter is typed, I want the letter icon to disappear, and reappear when it is erased. I have most of the functionality done already, but there are a few bugs I cannot fix. Right now I am using a KeyListener and filtering input/taking action based on key events. However, complications arise when multiple keys are pressed at the same time.

The input filtering isn't my main problem, it's putting the letters back in the bank when they are erased. Is there a way to remove the caret entirely and only allow users to add input to / backspace from the end of the JTextField? Also, any tips about handling button mashing correctly would be greatly appreciated. Thanks!

Note: If you've ever played Super Text Twist, that's basically the type of guessing method that I'm going for.

Upvotes: 2

Views: 227

Answers (1)

StanislavL
StanislavL

Reputation: 57381

You can extend PlainDocument (used in JTextField) and override insertString() and remove() methods. The yoou can check what's inserted or removed and adapt your interface.

Or (may be even better) use the DocumentFilter as suggested Hovercraft Full Of Eels

Upvotes: 2

Related Questions