Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: KeyListener or key binds?

  1. #1
    SYNYST3R1 is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default KeyListener or key binds?

    I am completely new to java and I have been teaching myself. I am trying to move an image with the keyboard. I have been having trouble with getting a KeyListener to work in a separate class and I don't understand key binds at all. Which is better to use? And is it okay to have it in a separate class? Will it even work or does it need to be in the main class because I know when I used c# it could be in a separate class but no matter what I do I can't seem to do it in java.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,556
    Rep Power
    23

    Default

    There are many code samples on this forum of both techniques. Do a Search for KeyListener and InputMap or ActionMap.

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

    Default

    You can do a KeyListener or key bindings in a separate class, so that's not the issue. As to which is better, I think it will depend on the situation. In my limited experience, key bindings are better for most situations since they are a higher level construct then KeyListeners, are how most Swing components handle key interactions, and are a bit more flexible when it comes to use when a bound component does not have focus.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    camickr has outlines of code for various tasks associated with key bindings at the Java Tips Weblog. There are links to Oracle's Tutorial pages near the end.

    I'm mostly posting this because the Java Tips Weblog is a real gem. It's not just a go-to place if you're stuck, but it's a model of careful code examples, description and discussion.
    Fubarable likes this.

  5. #5
    SYNYST3R1 is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    I am just completely stumped with key bindings and idk why. Usually I can figure things out on my own but this is tough for me. I just don't understand any part of them after looking at the tutorials I found.

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

    Default

    Quote Originally Posted by SYNYST3R1 View Post
    I am just completely stumped with key bindings and idk why. Usually I can figure things out on my own but this is tough for me. I just don't understand any part of them after looking at the tutorials I found.
    It seemed a little counter-intuitive to me at first, but if you're like me, you'll "get" it by experimenting with it over and over again. Don't give up.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Here's what I did. (guided heavily by the How to Use Key Bindings tutorial)

    (1) I created an ImageComp class. It extended JComponent and had three attributes: Image image (the image to be drawn) and int x, y (the upper left position of the image).
    (1-painting) I overrode the paintComponent() method so it did the right thing: drew the image at the x/y offset.
    (1-constructor-a) The constructor of this class was passed the image to be drawn (!). It also got the image and action maps to be used later.
    (1-constructor-b) I put() an entry into the input map that connected the left arrow key stroke with an action map key "left". Then I put() an entry into the action map that connected "left" with an action I called leftAct. This action didn't exist so the IDE grumbled. That's OK. I repeated this for the other directions. (Actually I didn't. I just did left first, then completed the next bits and tested it. I went back and added "right", "up" and "down" once I was convinced it would work)
    (1-actions) I added the actions that the IDE was grumbling about. Each was a subclass of AbstractAction and I only bothered overriding actionPerformed() - although the other methods could be useful - to increment/decrement x/y and then repaint(). These actions (together with the painting defined above) are all of the behaviour of the MyImage class.

    (2) Next I created a class to illustrate the ImageComp. This used SwingUtilities.invokeLater() to construct the gui on the event dispatch thread. (See Initial Threads and the numerous examples of this given there.) The gui just created a suitable image and used it to construct an ImageComp which was added to a frame. The frame was given an initial size and then made visible.

    --------------

    In this case the bindings and actions were all within the class whose behaviour was being controlled. I suppose they could have been made elsewhere if I wanted another class to "control" the area within which the image moved. (constrain it, eg)

    --------------

    I realise I could have saved time (and space) by posting the code. Hopefully this will be more useful.
    Last edited by pbrockway2; 06-19-2011 at 06:03 AM.

  8. #8
    SYNYST3R1 is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    Well I went ahead and remade my whole project and it worked so I'm guessing I just did something wrong along the way that made the keylistener not work in the separate class. I still don't understand key binds but I will work on them for future projects :)

Similar Threads

  1. Help with keylistener?
    By Kaizo in forum New To Java
    Replies: 4
    Last Post: 12-11-2010, 12:55 AM
  2. keyListener not doing anything
    By imorio in forum AWT / Swing
    Replies: 10
    Last Post: 08-17-2010, 10:46 PM
  3. KeyListener - Is this what I need?
    By dbashby in forum New To Java
    Replies: 26
    Last Post: 04-18-2009, 04:14 PM
  4. KeyListener Example
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:46 PM
  5. how to add a KeyListener
    By leonard in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 04: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
  •