Solved

Swing's JTextField and Tab KeyEvents

Posted on 1998-05-26
1
1,997 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

717 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