Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

When is DocumentListener invoked?  RSS feed

 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting some behavior with my TextField that I don't understand. Is the listener invoked when the TextField is changed with 'SetText' rather then the keyboard? If so how can I prevent that behavior or at least detect that condition to take appropriate action? TIA.
 
Fred Kleinschmidt
Bartender
Posts: 507
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just before you call setText(), just set a flag that that you can check in the listener's insertUpdate() method. Then reset it after the setText():

 
Rob Camick
Ranch Hand
Posts: 2753
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the listener invoked when the TextField is changed with 'SetText' rather then the keyboard?


The listener is invoked whenever the Document is changed.

The setText(...) method is just a convenience method for using textField.getDocument().insertString(...).

The same thing happens when you use the keyboard. The insertString(...) method of the Document is ultimately invoked after accessing the Document of the text field with focus.

Another approach is to remove the DocumentListener:


I prefer this approach as the listener code is not dependent on the state of some external variable.



 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I thought of that but I was hoping for something built-in that was more elegant.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!