[java] catch all changes to a JTextField

Started by
4 comments, last by snisarenko 15 years, 3 months ago
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.
Advertisement
Try this tutorial on JTextField:
http://java.sun.com/docs/books/tutorial/uiswing/components/textfield.html
I believe there is an example that listens for updates to the JTextField as the user types.
Yeah, I read that tutorial, it uses an action listener which only executes if the user hit the enter key, I need to know when the user modifies the field in any way.
I think that might have something to do with windowing. Look into window selection/deselection regarding text field losing focus or being tabbed away from.
Try using a KeyListener.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
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

This topic is closed to new replies.

Advertisement