Solved

Swing's JTextField and Tab KeyEvents

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
changePi Challenge 15 76
network + 7 73
countPairs challenge 7 58
java 8 lambda expresssions exception handling 3 38
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

760 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

20 Experts available now in Live!

Get 1:1 Help Now