Results 1 to 5 of 5
  1. #1
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    3

    Default Event Collision (What I call it) Problem.

    For an example if you have a program with one JPanel that is changing to "Blue" when klicking at the "B" button & "Red" when klicking at the "R" button. If you have a JButton (Or any kind of button) at the side of it, when you press it, with an ActionListener, the KeyListener stoppes working.

    The KeyListener is at the "Frame" object, wich is pretty much just a normal JFrame.

    Thanks!

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

    Default

    Your problem is a common KeyListener problem: KeyListeners can only work when the component that they've been added to have the focus. When you click on a button, whatever had the focus before loses it, and this causes the key listener to stop functioning.

    A good solution for this is to use key bindings instead. Please check out the tutorials for the details on how to use these: Key Bindings Tutorial

  3. #3
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    3

    Default

    Is there any possible way to reset the focus of the window back?

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

    Default

    Why not simply use Key Bindings as suggested?

  5. #5
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    3

    Default

    Okay, I read through the entire tutorial carefully. What I don't understand is where in the code I am supposed to put it & how it knows what key that you are pressing. Would be nice with a five row code quick example, just so that I understand.

Similar Threads

  1. Layout and button event problem! please help
    By counterfox in forum AWT / Swing
    Replies: 0
    Last Post: 01-28-2011, 12:48 AM
  2. Problem in collision between objects
    By Chetans in forum Java Gaming
    Replies: 3
    Last Post: 03-15-2010, 11:24 PM
  3. how to call other object's event
    By kirzo in forum New To Java
    Replies: 3
    Last Post: 07-11-2009, 12:23 PM
  4. Replies: 0
    Last Post: 07-07-2008, 07:10 AM
  5. problem with event handling!!!
    By ahdus in forum Java Applets
    Replies: 1
    Last Post: 11-17-2007, 06:24 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
  •