Results 1 to 7 of 7
- 11-20-2010, 10:48 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 73
- Rep Power
- 0
DocumentListener, KeyListener or a third option?
Hey.
I have a few JTextFields, currently with documentListeners.
Now, what I want to do is basically to check, inside the documentListener-methods, whether the input to the JTextField is done by the user, or by the code (via .setText(String)).
Should I just use the KeyListener instead or is there a third option that I'm not familiar with?
Thanks in advance!
-
Key listener won't work if you paste in text. A DocumentFilter would but I don't think it could distinguish between text from the app vs. the user. You could I suppose set a flag when the app changes the text and then unset the flag after the text has been changed.
- 11-20-2010, 11:08 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 73
- Rep Power
- 0
I was thinking of removing the documentListener and adding it again - but I have no idea if it will create flaws that I don't know of.
Since I'm not familiar with flags I'd very much appreciate further explanation or even better: a short example with a short explanation included.
-
I refer to having a boolean variable in the class that is set to true or false, nothing more.
Java Code:private boolean appUpdatingText = false; private void appUpdateField(JTextField textField, String text) { appUpdatingText = true; textField.setText(text); } //... then in the listener code if (appUpdatingText) { appUpdatingText = false; } else { //... do what you want to do if the user updates the field }Last edited by Fubarable; 11-20-2010 at 11:18 PM.
- 11-20-2010, 11:34 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 73
- Rep Power
- 0
-
- 11-20-2010, 11:58 PM #7
Member
- Join Date
- Nov 2010
- Posts
- 73
- Rep Power
- 0
Similar Threads
-
Use documentlistener to get source of which text field is changed
By Andolph23 in forum AWT / SwingReplies: 3Last Post: 05-28-2010, 04:18 AM -
JAX-WS\METRO - apt with option option key="r"
By Talkinjive in forum Web FrameworksReplies: 0Last Post: 01-31-2010, 04:15 PM -
KeyListener - Is this what I need?
By dbashby in forum New To JavaReplies: 26Last Post: 04-18-2009, 04:14 PM -
KeyListener Example
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:46 PM -
how to add a KeyListener
By leonard in forum New To JavaReplies: 1Last Post: 08-06-2007, 04:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks