Results 1 to 8 of 8
Like Tree2Likes
  • 2 Post By CuppaCoffee

Thread: Detecting A Key Action in a JTextField

  1. #1
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default 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!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default 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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  5. #5
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default 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~!

  6. #6
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default 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..

  7. #7
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Detecting A Key Action in a JTextField

    Quote Originally Posted by CuppaCoffee View Post
    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..
    It is a method of Document, which you must get from the JTextField through the getDocument() method

  8. #8
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default 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.
    DarrylBurke and Fubarable like this.

Similar Threads

  1. Detecting second application action's
    By ContoNick in forum New To Java
    Replies: 5
    Last Post: 12-30-2011, 06:33 PM
  2. Replies: 1
    Last Post: 06-30-2011, 09:49 AM
  3. Replies: 3
    Last Post: 08-19-2010, 08:27 AM
  4. Replies: 1
    Last Post: 01-30-2009, 06:44 PM
  5. [SOLVED] action listener and Jtextfield
    By tOpach in forum AWT / Swing
    Replies: 4
    Last Post: 12-16-2008, 01:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •