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

Thread: Question about KeyListeners

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Location
    Don't worry about it.
    Posts
    14
    My Mood
    Relaxed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Question about KeyListeners

    Does anyone know if it is possible to add a key listener to an object that is not a Component or a subclass of a component?
    I would like to add a key listener to a self defined class, but I'm not sure it can be done.

    Thanks,

    Jams


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Question about KeyListeners

    Yes you can add code to you class for a listener. What class does your class extend?
    If you don't extend any Swing class that has a listener, then your class will have to write ALL the code to support the listeners. If that is the exercise you are trying to do, then it can be done. You'll have to convince the JVM that your class can get the focus and to have the JVM give your class the focus.
    If you don't want to write all the code needed to support a listener, then extend a class that does support the listener.

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Location
    Don't worry about it.
    Posts
    14
    My Mood
    Relaxed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question about KeyListeners

    I'm creating a class to represent a player in a very small game. The Player class extends Object and has a whole slew of functionality in the works. Originally, I created a Controller class that implemented KeyListener and was activated by a few characters. The Controller class, on key press, began a switch statement that would check for characters, w, a, s, and d. Depending which character was pressed, the appropriate player method was called. I have a third class that I call Canvas. It is a self repainting JPanel that will run at a defined fps. The canvas is using Controller as a key listener. This worked GREAT considering it was a rapid prototype.

    However, I would much rather prefer the Player class be independent of two other classes. Right now the canvas receives a key and passes it to the controller which tells the player which way to move. The player has private data to keep track of it's location and it's drawToGraphics() method uses these data members when displaying.

    Any advice on where to start?

    Thanks,

    Jams

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Question about KeyListeners

    I would much rather prefer the Player class be independent of two other classes.
    Please explain?

    Unless you are interested in learning how to write your own Swing classes framework, you'll be better off extending an existing class that does much of the GUI work for you.

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Location
    Don't worry about it.
    Posts
    14
    My Mood
    Relaxed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question about KeyListeners

    Well, the Player class is what I would like to add these KeyListeners to. That way the The Player receives a KeyEvent and respond accordingly.

    Right now the Canvas has a KeyListener, Controller. Controller implements KeyListener and it's constructor requires a Player. So, when a key is pressed, the Controller calls the appropriate function on the Player that was passed in the constructor.

    I would like to add the key listener to the Player class so that it will not have to rely on the Canvas to pass input to the Controller to make the the corresponding player move. I would like to have the player have a listener to eliminate the need for the separate controller class to move the player.

  6. #6
    Junior Member
    Join Date
    Jun 2011
    Location
    Don't worry about it.
    Posts
    14
    My Mood
    Relaxed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question about KeyListeners

    Thank you for your help Norm. I have it working in a more manageable way now!

Similar Threads

  1. KeyListeners: Automatic Focus?
    By bgroenks96 in forum Java Theory & Questions
    Replies: 32
    Last Post: June 24th, 2011, 09:03 PM