Solved

Swing's JTextField and Tab KeyEvents

Posted on 1998-05-26
1
1,963 Views
Last Modified: 2008-02-01
I am using Swing's JTextField and I am having difficulty listening for a Tab KeyEvent.  I am adding a KeyListener to the JTextField, and I can listen for most KeyEvents fine, but it doesn't seem to detect when a Tab KeyEvent is sent.  I have tried on keyPressed, keyReleased, and keyTyped and none of them detect a Tab KeyEvent on JTextField.  How can I get this to work?
0
Comment
Question by:bjg
1 Comment
 
LVL 1

Accepted Solution

by:
dufort earned 50 total points
ID: 1221495
To intercept TABs or special keys, you must subclass a component and set its isFocusTraversable() and isManagingFocus() so that they return false and true. Here is a sample subclass for JTextField:

  import com.sun.java.swing.*;
  import com.sun.java.swing.event.*;
  import java.awt.*;
  import java.awt.event.*;

      public class JTabTextField extends JTextField implements KeyListener
      {
            JTabTextField(int width)
            {
                  super(width);
            }
            
            public boolean isFocusTraversable()
            {
                  return false;
            }
            
            public boolean isManagingFocus()
            {
                  return true;
            }

            //from interface KeyListener
            public void keyPressed(KeyEvent ke){}
            public void keyReleased(KeyEvent ke){}
      
            public void keyTyped(KeyEvent ke)
            {
                  if(ke.getKeyCode() == KeyEvent.VK_TAB)
                  {
                        System.err.println("{TAB}");
                  }
            }
      }
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now