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

JTextField changed, last to know  RSS feed

 
Paul Bailey
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i have a JTextField and after it changes i want to be the last to know (i.e. i want every other method based on this text field chaning to have done its business, what should i do?
The problem is that by addint a KeyListener, i have get informed before the text in the textfield gets informed. How exasperating.
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,
You have 2 choices:
1. Implement ActionListener. This requires user to hit return but you will then know that all text is complete.
2. Subclass JTextField yourself, use enableEvents method and override the processXXXXXEvent. Then you can call all registered users first and do your stuff later.
NOTE: For choice 1, you can't know when you will be called in the listener chain (JVM doesn't guarentee anything). For choice 2, you are sure to be the last listener to handle the event.
Regards,
Manfred.
 
Paul Bailey
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help Manfred. I tried method 1 and found it confused even the best informed user (me). While considering method 2 i came across http://java.sun.com/docs/books/tutorial/uiswing/components/textfield.html#documentlistener the use of a Document Listener to perform this. It appears to be the last to know, so everything worked great!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!