Results 1 to 4 of 4
Thread: KeyListeners not working
- 02-06-2011, 02:17 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
KeyListeners not working
Hello everyone, I'm kind of a newbie to the swing interface, and this is my first post in the java forums. So, I apologize if I'm making a very stupid/obvious mistake.
My project set up right now has a Jframe, a custom class that extends Jcomponent, and a TextArea. My custom Jcomponent implements KeyListener, and uses the keyPress function as part of its purpose. When I have just the JComponent in the JFrame, the key events (as well as mouse and timer events) work fine. But when I add the textarea to the frame, the keylisten events (but not the mouse or timer events) stop being called.
My first thought was that my component wasnt in focus. So for testing I disabled focusing on my TextArea, and made my custom class constantly calling the requestFocus() function. Debugging revealed that this was succesful, and that the currently focused object was always my jcomponent. But still, the keyboard events didnt work!
Does anyone know what could be causing this, or what I can do to fix this? Thank you for your help!
-
If at all possible, you'll be better off using Key Bindings rather than a KeyListener, and there's a great tutorial on this at the Java Swing tutorials.
- 02-06-2011, 02:55 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
Those are really interesting actually. I like the idea of having actions; feels like it would be a lot cleaner than a bunch of esoteric conditionals. Thanks a bunch! :)
I haven't done much playing around with them yet, but I think it can handle most of what I'm trying to do.
Although I'm still curious as to what I'm doing wrong with keyListeners/focus, if anyone has any ideas. :/
-
A problem may be that your component is not focusable, that is, it's focusable property is false, and if so, even if you request the focus, it isn't able to obtain it. You can change this by calling setFocusable(true) on the JComponent.
Similar Threads
-
\n not working in GUI (working code, but \n isn't working)
By cc11rocks in forum New To JavaReplies: 2Last Post: 01-04-2011, 04:30 AM -
Problem with keylisteners
By dunafrothint in forum AWT / SwingReplies: 2Last Post: 11-23-2009, 05:54 PM -
[SOLVED] KeyListeners in JavaApplets
By Unome in forum Java AppletsReplies: 18Last Post: 10-30-2008, 10:36 PM -
Java mail problem(working in intranet,but not working in iternet)
By sundarjothi in forum Advanced JavaReplies: 8Last Post: 05-28-2008, 07:00 AM -
Working With ANT
By JavaForums in forum EclipseReplies: 0Last Post: 04-26-2007, 08:16 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks