Thank you for your question. java awt code can certainly exhibit
counterintuitive behavior, particularly when older APIs must be
supported.
Thanks particularly for exhibiting a small and self-contained example.
The problem here is that the keypress is interpreted as a
focus-change.
In order to trap tabs, the simplest say is to invoke
setFocusTraversalKeysEnabled(false)
on your component. From the documentation for this method:
---
public void setFocusTraversalKeysEnabled(boolean
focusTraversalKeysEnabled)
Sets whether focus traversal keys are enabled for this Component.
Components for which focus traversal keys are disabled receive key
events for focus traversal keys. Components for which focus traversal
keys are enabled do not see these events; instead, the events are
automatically converted to traversal operations.
--
Of course, you can get finer control over exactly which focus
traversal keys you want to enable or disable, but likely you do not
need this.
It is worth noting as well the keyDown() is deprecated anyway.
However, sometimes when dealing with old java platforms, one wants to
use deprecated APIs.
I tried your code with a new main method:
public static void main( String args[] )
{
SimpleFrame frame=new SimpleFrame();
frame.setFocusTraversalKeysEnabled(false);
frame.setVisible(true);
}
and obtained the expected behavior.
(I was also able to replicate the original problem).
I am using JDK 1.4 on windows 2K. Obviously for a real QA one has to
test on all versions of the JDK and on all platforms to which one is
deploying; however, I expect this will work on JDK1.1 as it does not
use the newer 1.4 APIs.
Search strategy:
---------------
The main java api is at: http://java.sun.com/j2se/1.4.1/docs/api/
I have this downloaded locally of course; it's quite handy.
I also checked the java bug parade at
http://developer.java.sun.com/developer/bugParade/
for which registration is required. I searched there on:
+keyDown +tab
although I did not find anything useful.
I did standard google searches as well on keydown tab java, and looked
through some tutorials; I did not find anything apposite.
I did recompile with -deprecation to get a message
SimpleFrame.java:9: warning: keyDown(java.awt.Event,int) in
java.awt.Component has been deprecated
public boolean keyDown( Event e , int key ) {
1 warning
The actual solution though was just found by looking through the
documentation for java.awt.Component. It was substantially complicated
by the fact that the whole focus API changed in 1.4, in the words of
the java documentation:
"Prior to Java 2 Standard Edition, JDK 1.4, the AWT focus subsystem
was inadequate. It suffered from major design and API problems, as
well as over a hundred open bugs" [From the AWT Focus Subsystem]
However, you specified that solution must work on JDK 1.1.
----
If you would like additional explanation, or if the solution is not
adequate in any way, please use the "Request Clarification" button to
solicit clarification before rating the answer. |