Results 1 to 8 of 8
- 12-21-2012, 10:04 PM #1
Detecting A Key Action in a JTextField
Hello, friends. It has been quite a while since I've seen all you eager helpers on this forum. How have you all been?
I have been dandy, and by dandy, I mean I need some assistance. Now, to keep this brief and simple, all I'm trying to do is to get a program to detect when someone is typing in a JTextField. So the moment you press any key within this JTextField, it should be aware.
How would I do this? Do I need a certain type of Listener for the TJextField? Is there a function or method that does this already? Any help would be appreciated, and Happy Holidays!
-
Re: Detecting A Key Action in a JTextField
This is usually not something that you'd want to check -- for instance what if the user changes text in the field via the mouse or via cut and paste? Instead often it is better to get the JTextField's Document and either add a DocumentListener to it to listen for changes after the fact, or a DocumentFilter to listen for Document changes before the fact.
- 12-21-2012, 10:47 PM #3
Re: Detecting A Key Action in a JTextField
You make a very good point, but fortunately, that isn't a problem with the program I'm making.
You see, I'm making a very simple game of Arkanoid, but at the start screen, it has a "Level Select" option, where if you type in the right code, you can go to a specific level after pressing 'Play'. For this program, you aren't able to cut/paste anything with the mouse, the only way to input/remove text is through the keyboard. What I need to happen, is that if the person is typing (WHILE they are typing) the write code, the foreground of the whole thing will turn blue. Otherwise, it will stay black. So with every character they input, or every time they hit the backspace button, I need to check if the string equals a valid password.
Strict details and rare exceptions aren't important, because this is not a huge aspect of the game as a whole.
-
Re: Detecting A Key Action in a JTextField
It doesn't matter. KeyListeners are low level and should be avoided. All of this can be done with a DocumentListener and again I recommend that this is what you use.
- 12-21-2012, 11:40 PM #5
Re: Detecting A Key Action in a JTextField
KeyListeners are low level..? So what is a higher degree than a KeyListener, in terms of functions with keys?
But if you insist DocumentListener is better, than I'll try that out. Thank you very much for the help, Fubarable! You always give so much insight.
Merry Christmas~!
- 12-22-2012, 12:26 AM #6
Re: Detecting A Key Action in a JTextField
Hmm.. I'm sorry to bug you again, Fubarable. But I don't see a method available for a DocumentListener..? As in, JTextField.addDocumentListener(DocumentListener l) isn't available.. nor is it available for the JPanel/JFrame I'd be using..
- 12-22-2012, 12:35 AM #7
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
- 12-22-2012, 12:47 AM #8
Re: Detecting A Key Action in a JTextField
And this will allow me to detect when someone presses any key within the JTextField?
Edit: Nevermind, I've managed to figure it out. I've used my JTextField with the method .getDocument() (as you said), and afterwards used .addDocumentListener(), like you guys mentioned as well.
Then with the implemented methods, I can see if text has been inputted or removed, and act depending on that.
Again, I am sincerely thankful for the help, guys. Just took a bit of tinkering with to understand and incorporate what you guys were talking about. Sorry if I was any trouble, and thank you.Last edited by CuppaCoffee; 12-22-2012 at 12:58 AM.
Similar Threads
-
Detecting second application action's
By ContoNick in forum New To JavaReplies: 5Last Post: 12-30-2011, 06:33 PM -
HTTP Status 404 - There is no Action mapped for namespace / and action name
By kurakukiran in forum Web FrameworksReplies: 1Last Post: 06-30-2011, 09:49 AM -
HTTP Status 404 - There is no Action mapped for action name showmainframe...
By vaibhavspawar in forum Advanced JavaReplies: 3Last Post: 08-19-2010, 08:27 AM -
how to access jTextField of one JFrame1 from JFrame2 & Modify JTextField contents
By sumit1mca in forum AWT / SwingReplies: 1Last Post: 01-30-2009, 06:44 PM -
[SOLVED] action listener and Jtextfield
By tOpach in forum AWT / SwingReplies: 4Last Post: 12-16-2008, 01:02 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks