Google Answers Logo
View Question
 
Q: How can I detect the TAB key in a Java AWT Frame? ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: How can I detect the TAB key in a Java AWT Frame?
Category: Computers > Programming
Asked by: bigjosh-ga
List Price: $25.00
Posted: 19 Dec 2002 09:15 PST
Expires: 18 Jan 2003 09:15 PST
Question ID: 126916
I have a straight Java AWT Frame and I want to process the TAB key.
Currently I override the keyDown() method to get keypresses, but this
does not work for the TAB key. I've tried various examples from the
web
that involve processKeyEvent(), KeyListeners, and action(), but none
seem to work on
my Windows XP machine with JDK 1.4.1.

I am only interested in an AWT-based solution that is JDK 1.1 - 1.4
compatable (no Swing).

If possible, please *try* your solution before posting it to make sure
it actually works.

Request for Question Clarification by answerguru-ga on 19 Dec 2002 09:49 PST
Hi bigjosh-ga,

Do you mind posting your source code here so there is something to
work with? That would be appreciated :)

answerguru-ga

Clarification of Question by bigjosh-ga on 19 Dec 2002 10:15 PST
Here is the simpletest example. If you run this program, it will print
a message each time you press a key - except for the TAB key. I want
to be able to print a message when the TAB key is pressed also.

===
import java.awt.Frame;

import java.awt.Event;

public class SimpleFrame extends Frame
{
	
	
	public boolean keyDown( Event e , int key ) {
		
		System.out.println( "Key pressed = " + key );
		
		return true;
		
	}
	
	
	public static void main( String args[] ) throws InterruptedException
{
		
		new SimpleFrame().setVisible(true);
				
	}
	
}
===

Clarification of Question by bigjosh-ga on 19 Dec 2002 10:24 PST
Ignore that "throws InterruptedException" on the main() - it is
unnessisary and an cutting/pasting artifact.
Answer  
Subject: Re: How can I detect the TAB key in a Java AWT Frame?
Answered By: rbnn-ga on 19 Dec 2002 13:23 PST
Rated:5 out of 5 stars
 
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.
bigjosh-ga rated this answer:5 out of 5 stars and gave an additional tip of: $15.00
Prefect!

I added the setFocusTraversalKeysEnabled(false) as suggested and
started getting the TAB events I wanted.

Thanks!

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy