Results 1 to 8 of 8
Thread: KeyListener or key binds?
- 06-19-2011, 03:12 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 3
- Rep Power
- 0
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.
- 06-19-2011, 03:21 AM #2
There are many code samples on this forum of both techniques. Do a Search for KeyListener and InputMap or ActionMap.
-
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.
- 06-19-2011, 04:16 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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.
- 06-19-2011, 04:52 AM #5
Member
- Join Date
- Jun 2011
- Posts
- 3
- Rep Power
- 0
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.
-
- 06-19-2011, 06:01 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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.
- 06-19-2011, 06:33 AM #8
Member
- Join Date
- Jun 2011
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Help with keylistener?
By Kaizo in forum New To JavaReplies: 4Last Post: 12-11-2010, 12:55 AM -
keyListener not doing anything
By imorio in forum AWT / SwingReplies: 10Last Post: 08-17-2010, 10:46 PM -
KeyListener - Is this what I need?
By dbashby in forum New To JavaReplies: 26Last Post: 04-18-2009, 04:14 PM -
KeyListener Example
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:46 PM -
how to add a KeyListener
By leonard in forum New To JavaReplies: 1Last Post: 08-06-2007, 04:44 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks