1

Trying to get the value of a JTextField as it's being updated (chars inserted, chars removed, chars changed) and then setting it's value.

I've run into the "Attempt to mutate in notification" exception which lead me to:

However, you should never modify the contents of a text component from within a document listener. If you do, the program will likely deadlock. Instead, you can use a formatted text field or provide a document filter. from here

I'm using a document filter to limit the number of chars of the JTextField to 12 but I don't know how I can detect changes on the JTextField without using the document listener...which is the source of the exception

Any advice or SSCCE for me?

Strokes
  • 157
  • 7
  • 23
  • 3
    http://stackoverflow.com/questions/3519151/how-to-limit-the-number-of-characters-in-jtextfield – posdef Mar 10 '15 at 14:28
  • 2
    also: http://stackoverflow.com/questions/6172267/how-to-restrict-the-jtextfield-to-a-x-number-of-characters?lq=1 – posdef Mar 10 '15 at 14:29
  • @posdef As well as limiting the characters I want to fire an event on remove/add/change characters.. – Strokes Mar 10 '15 at 15:12
  • Use the second link. The `DocumentFilter` is the newer preferred approach. – camickr Mar 10 '15 at 15:14
  • `I want to fire an event on remove/add/change characters` - why? Do you mean "fire" or "listen" for an event? In any case the DocumentListener is invoked for additions and removals of text. – camickr Mar 10 '15 at 15:16

1 Answers1

2

It's simple. Wrap the logic of DocumentListener's events into SwingUtilities.invokeLater() to prevent Attempt to mutate in notificatio problem

StanislavL
  • 56,971
  • 9
  • 68
  • 98