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
Solved

Swing's JTextField and Tab KeyEvents

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example 11 49
Convert from a json string array to a Java object 3 53
Adding multiple JVM environments to RedHat 6 7 47
Tagging and Merging on Branch 1 29
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

860 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