Sign in to follow this  
NumberXaero

[java] catch all changes to a JTextField

Recommended Posts

I added an ActionListener but the code that reads from the text field is only triggered when/if enter is pressed after typing. I also tried placing the read from text field code in a DocumentListener and each of its three overload functions but then it only triggered when the adjacent associated tree node chnaged. How do I trigger code to run when the text in the field changes? when text field looses focus, text is changed, or field is tabbed away from.

Share this post


Link to post
Share on other sites
Quote:
Original post by NumberXaero
How do I trigger code to run when field is tabbed away from, or when text field looses focus


Use a FocusListener.

textField.addFocusLister(myFocusListener);

Focus Listener Reference

Quote:
Original post by NumberXaero
How do I trigger code to run when ... when text is changed,


http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JTextField.html

Quote:
In the JTextComponent based components, changes are broadcasted from the model via a DocumentEvent to DocumentListeners. The DocumentEvent gives the location of the change and the kind of change if desired.


Use a DocumentListener.

textField.getDocument().addDocumentListener(myDocListener);

Document Listener Reference

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this