Solved

Swing's JTextField and Tab KeyEvents

Posted on 1998-05-26
1
1,987 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
spring jars download 1 53
Android development question 2 79
How to configure empty element in XML Document parser? 15 44
American Express @Work site and Java 4 67
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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

749 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