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 3 of 3

Thread: KeyListeners Require Focus...

  1. #1
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Question KeyListeners Require Focus...

    In a program I'm writing, I use a KeyListener to perform certain actions. However, I tried adding a KeyListener to the entire top level JFrame, and no events were received. When I had the JFrame request focus, the KeyEvents were received. However, I couldn't tell the JFrame to constantly request the focus, because then a user would not be able to minimize the JFrame or activate another window, as the JFrame would immediately pop up (due to the requesting of the focus).

    So, what I tried next was adding the KeyListener to a JPanel, then having that JPanel constantly request focus. However, this caused another problem: other JComponents in the JFrame could not adequately perform their functions because the JPanel requesting focus would steal the focus from the JComponents prematurely...

    So, for the meantime I have removed the KeyListener, but I am still irked about its performance (and my lack of knowledge). Is there any way to have a KeyListener receive KeyEvents without the host JComponent having the focus?

    Any help or suggestions would be appreciate. Thanks!
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.


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

    Default Re: KeyListeners Require Focus...

    Check out how to use KeyBindings....they are probably more appropriate for your situation
    How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

  3. The Following User Says Thank You to copeg For This Useful Post:

    snowguy13 (January 6th, 2012)

  4. #3
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: KeyListeners Require Focus...

    Check out how to use KeyBindings...
    Ah, yes, this is perfect! Thank you!
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

Similar Threads

  1. Question about KeyListeners
    By Jams in forum Java Theory & Questions
    Replies: 5
    Last Post: November 6th, 2011, 10:08 AM
  2. KeyListeners: Automatic Focus?
    By bgroenks96 in forum Java Theory & Questions
    Replies: 32
    Last Post: June 24th, 2011, 09:03 PM
  3. Remove focus from JSpinner?
    By hafunui in forum AWT / Java Swing
    Replies: 4
    Last Post: June 16th, 2011, 04:17 PM
  4. Code require for Drag and drop
    By anujbatta in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 25th, 2011, 07:54 AM
  5. Replies: 5
    Last Post: December 13th, 2009, 07:10 PM

Tags for this Thread