Skip to main content

Change in TAB key functionality with Java 1.7?

1 reply [Last post]
Joined: 2007-01-11
Points: 0

I am a relatively beginning Java programmer. I have a few years experience trying to maintain and upgrade a large Java GUI app that interacts with a backend database.

There are many screens in the app, with Textfields inside of Frames, and these components have been inherited by classes in this app to customize the fields/frames.

Traversal amongst the fields was functioning properly until 1.7 came along. Before I made any changes trying to correct it, the TAB key had become unresponsive. It did nothing, or so it would appear. After further inspection, I found that it was actually gaining focus on the 'next' field, and then immediately losing focus, and regaining focus on the original field.

I made one change to a section of code that had been put in place to handle changes encountered with the release of 1.4. It was inspecting the java vendor/version and deciding on executing a code block. It was looking for a vendor string of 'Sun', so now with 1.7, it was not getting a true condition anymore. I changed that so it is now executing the code it was previously for versions 1.4-6.

Anyway, the code it is executing calls setFocusTraversalKeysEnabled(false) on each component. Now, with this enabled for 1.7, the TAB key does in fact go (or tries to go) to the next field, but before it does that, it inserts the TAB character. This usually results in the field failing its format check.

Can anyone lend some insight into why this is happening, or point me to some documentation that might explain it?


Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2007-01-11
Points: 0

Perhaps this will help others with a similar problem...

After much debugging I believe I have found a solution. I found that the code running between Java 1.6 and 1.7 ran exactly the same way through my application's code and through the built-in Java classes as well. I traced the moment when the tab was inserted into the TextField down to the dispatchKeyEvent method in the DefaultKeyboardFocusManager class. When it issued the command peer.handleEvent(Event e), the tab was inserted for 1.7, but not 1.6. This was during the tail end of the KEY_TYPED event.

So, in my code, where I am handling the KeyEvents, I inserted the following code:

if (e.getID() == KeyEvent.KEY_TYPED)
if (((KeyEvent)e).getKeyChar() == KeyEvent.VK_TAB)
if (my_parent.Session.CW.version.compareTo("1.6") > 0)

Disregarding the variables specific to my application, essentially I am reading the keyChar attribute of the KeyEvent, checking if it is a tab, and then if the Java version is past 1.6, I am consume'ing the event.

The keyChar attribute is the only attribute that can be used as everything else is undefined or otherwise not associated with the tab key at this point. keyCode for instance, was set to 0. More about this can be read about in the 1.7 doc's regarding java.awt.event.KeyEvent.

Anyway, helped me, hopefully someone else can use it.