Results 1 to 7 of 7
  1. #1
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Event Listenner hep

    Hi, I am simply trying out game concepts and am currently wanting to use event handling. However i know the code to use but i am not sure of the 'object' to listen to. This is what i mean:
    Java Code:
    SOMEOBJECT.addKeyListener();
    My questioin is; i i have a game where i want to simply move an image using the arrow keys, what object should be listened to?

    EDIT: Sorry about the title, im hopeless at proofreading.
    Last edited by jammas615; 07-05-2011 at 04:38 AM. Reason: Title

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Any class that extends (directly or indirectly) Component.

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

    Default

    If using KeyListeners, then the object listened to must be the component that has or should have focus. If you want greater flexibility in this regard, then use Key Bindings instead. There's a decent tutorial on this in the Oracle Swing tutorials.

  4. #4
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default

    So should i listen to my window?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    If it has the focus. Write a small program with a window, add listeners with printlns to show they were called, compile and execute and see what happens. There are several sample programs on the forum if you used Search.

  6. #6
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default

    Is focus the method currently be used? Or being used when listening

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Focus is the term to refer to which window is currently active. You can call the requestFocus method if the window doesn't currently have focus.

Similar Threads

  1. key event help
    By Kyle227 in forum New To Java
    Replies: 6
    Last Post: 05-09-2010, 07:01 AM
  2. click event
    By raman_v in forum Java Servlet
    Replies: 2
    Last Post: 01-08-2010, 01:05 PM
  3. checking for an event during an event
    By infinity in forum AWT / Swing
    Replies: 22
    Last Post: 04-09-2009, 02:08 AM
  4. Key event
    By ivvgangadhar in forum Threads and Synchronization
    Replies: 1
    Last Post: 12-11-2008, 10:27 AM
  5. Event
    By nt5515 in forum New To Java
    Replies: 0
    Last Post: 02-15-2008, 05:44 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
  •