wood burning stoves 2.0
The moose likes Java in General and the fly likes event.VK_TAB won't work with KeyListener in JPanel? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "event.VK_TAB won Watch "event.VK_TAB won New topic

event.VK_TAB won't work with KeyListener in JPanel?

Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169
I have a KeyListener within my JPanel parent class within a file. This JPanel is held within another file containing my JFrame parent window.
Within this KeyListener, I have

The problem I am having is that the tab key is not recognized as an action for the KeyListener. I know this simply by placing a

within the KeyPressed function within the KeyListener.
I have used this same method within a Login file I also use with this program. However, the Login extends Dialog whereas this file which is giving me problems extends JPanel. I don't know if that is the reason why TAB isn't registered as a key stroke. I don't know why it would, but you never know.
The KeyListener in question is listening to the parent (which remember extends JPanel). This is because nothing on the screen has actual editable fields, I populate them all via JLabels and Strings (complicated, yes, but it is how I chose to do it). My KeyListener in the Login Dialog is also just listening to the parent... which is, in that case, a Dialog.
Please help! I have asked 8 friends of mine and no one can figure it out. If I change VK_TAB to VK_F5 or something else, it works just fine. But I don't want to use F5!
Thanks in advance for any help or suggestions. I am completely baffled!
Matt Senecal
Ranch Hand

Joined: Dec 01, 2000
Posts: 255
Shouldn't this be

Also, can you post the offending code?
Originally posted by Jeff Grant:
Within this KeyListener, I have

There's nothing more dangerous than a resourceful idiot. ---Dilbert
Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169
Here is some of my additional code.

The problem is that the KeyPressed function never gets envoked when tab is pressed AT ALL. Neither the "any key was pressed" is output nor is the "Tab Pressed". I cannot figure out why. Thus since the "Tab Pressed" is not output, I obviously cannot use the real code in that if.
Suggestions please?
[ June 19, 2002: Message edited by: Jeff Grant ]
Clarence Kirby

Joined: Jun 18, 2002
Posts: 16
Why do you have a MainKeyHandler class inside of your Example class? Wouldn't it just be easier to implement KeyListener in the ExampleClass?
Also, what Matt said above I agree with. It's at least more readable and understandable if you use class constants from the class itself, not an instance of the class.
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
What version of Java are you using and what OS?
There is a listed Bug in 1.4, at least under Win98 and Solaris.

With the Java 2 Plug-in, a Java applet does not receive
java.awt.event.KeyEvent.VK_TAB events in the
keyPressed(KeyEvent e) method, despite the fact that the
applet has the keyboard focus. All other keyboard events
appear to be passed in properly -- there may be other
exceptions, but I have not found any.
This behavior is inconsistent with the Netscape Navigator implementation of applets, and also inconsistent with the behavior of applets when they are run in Internet Explorer under Microsoft's VM for Java.

Re-assigning this bug to classes_awt team.
xxxxx@xxxxx 2002-05-15

[ June 19, 2002: Message edited by: Cindy Glass ]

"JavaRanch, where the deer and the Certified play" - David O'Meara
Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169
Originally posted by Cindy Glass:
What version of Java are you using and what OS?

Thanks. That looks like my problem exactly. I posted my comments about what is happening to my program on Sun's site as well as someone else who is having MY same problem. Knew you guys would come through for me! I figured I was just missing something. as usual.
btw, I'm running Win 2k Pro and using Visual Cafe 4 for a compiler.
[ June 19, 2002: Message edited by: Jeff Grant ]
I agree. Here's the link: http://aspose.com/file-tools
subject: event.VK_TAB won't work with KeyListener in JPanel?
It's not a secret anymore!