Results 1 to 2 of 2
  1. #1
    jamhead is offline Member
    Join Date
    Dec 2010
    Rep Power

    Default Adding a KeyListener to a JFrame with buttons.

    Hey, I haven't been learning java very long so this may be really easy.
    We have a project and it involves inputting and encrypting a message. I don't want to add a keylistener to a JTextbox or anything similar as then the message I input is plainly viewable. I was hoping to be able to simply type when the frame is up and encode the text I type.

    This won't seem to work though in the window I want it to work in. It does work when i add the keylistener to an otherwise empty frame. But on the frame with buttons on it it won't work.
    Any help? Need to see the code?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    For a key listener to work, the component that holds it must have focus, and this is the reason why you're having problems when there are buttons on your JFrame. One solution is to make your listening component focusable and then give it the focus. Another solution is to use key binding.

    Myself, though, I like seeing what I type to be sure that there are no mistakes, and then converting the document, with an exception for passwords.

Similar Threads

  1. Help: Displaying Images AND Adding Buttons
    By Rhez in forum New To Java
    Replies: 2
    Last Post: 08-05-2010, 07:19 AM
  2. Help me in adding keylistener
    By kumarv75 in forum CLDC and MIDP
    Replies: 0
    Last Post: 06-22-2010, 08:10 AM
  3. [SOLVED] Buttons in JFrame
    By Nemo1959 in forum New To Java
    Replies: 8
    Last Post: 09-15-2008, 07:57 PM
  4. Replies: 0
    Last Post: 12-17-2007, 10:46 AM

Posting Permissions

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