Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Strange issue with Key Listener and Applets

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Strange issue with Key Listener and Applets

    I've been very puzzled about this for several hours trying to find out what is going on and I'm at a loss on this one.

    For some reason any time I use a Key Listener in a Java Applet the key listener won't function but it works fine in a JFrame.
    To top this off an asteroids game I made a while back has stopped working because of this and all the code in it is fine and unchanged. This strange issue just popped up and I'm not sure how long this has been going on since I just noticed it when trying to use an applet with a key listener in it.

    I'm not getting any errors and I have no leads on what is causing this either. Can someone help?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Strange issue with Key Listener and Applets

    Be sure the component that has the KeyListener registered has focus. An alternative is to use KeyBindings
    How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
    If this doesn't help, post an SSCCE that demonstrates the problem

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Strange issue with Key Listener and Applets

    Thanks I'll look.

    Any ideas on what could cause Key Listeners to suddenly stop working like this?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Strange issue with Key Listener and Applets

    what could cause Key Listeners to suddenly stop working
    You changed the code.
    Normally programs continue working the same way if they are not changed.
    Look at the changes you made.

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Re: Strange issue with Key Listener and Applets

    Found out what it was. For some reason I have to click on the applet before it'll let me do anything with the listeners.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Strange issue with Key Listener and Applets

    Look at the component class. It has methods for getting the focus.

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Strange issue with Key Listener and Applets

    Any ideas on what could cause Key Listeners to suddenly stop working like this?
    Applets run within a web browser, and how that web browser interprets focus can change depending upon the browser (both brand and version - in other words, update your browser and focus can change). This could potentially happen regardless of how many times you call request focus from the applet. If you need to change your code, then I'd again recommend trying key bindings. If not, there are ways to use javascript to try and give the applet focus.
    Last edited by copeg; January 31st, 2012 at 09:25 PM.

Similar Threads

  1. Strange errors with java.nio.file
    By jnewb in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 4th, 2012, 02:37 PM
  2. something strange
    By frozen java in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 4th, 2011, 08:58 PM
  3. Strange error message
    By javapenguin in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 11th, 2011, 02:03 PM
  4. Strange problem with drawing string
    By Asido in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 26th, 2010, 03:38 PM
  5. Strange Compiling Error
    By crism85 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 13th, 2009, 12:59 AM