Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Other Java > Java GUI
Java GUI Discussions specific to programming Java GUI.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java GUI section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old January 29th, 2007, 04:41 PM
Authorized User
Join Date: Nov 2005
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Cannot get panel to respond to tab key

Hi everybody.
I am trying to make an application where a panel object must respond to keyboard input so that, if the panel object is in focus, what happens next depends on which key or set of keys the user presses. I have almost made a panel respond to all types of characters from the keyboard exactly as required except for just one type of keyboard character: I cannot get a panel object to respond at all to the tab character.

A simplified version of my program is shown below. What this does when it runs is to display a JFrame object with a green coloured panel completely filling it. Then if you click on this panel, it gains focus and shows this by changing its colour to blue. Then you are supposed to be able to press almost any key on the keyboard and it will give some sort of response by printing something to System.out depending on which key you press.

This program consists of three top-level classes: MainClass that has the main method: MyFrame that extends JFrame and is used to hold the panel object: and KeyInPanel that extends JPanel and is used for making the green panel to be displayed in the MyFrame object. The KeyInPanel class has an inner class called PanelKeyListener that extends KeyAdapter class and the KeyInPanel constructor add an instance of PanelKeyListener to the KeyInPanel object so that it will respond to keyboard input.
The keyPressed(KeyEvent e) method in the inner class panelKeyListener overrides the method inherited from the KeyAdapter class and so this method should be always called by the system in response to a key event once the KeyInPanel object displayed on the MyFrame object is in focus.

But the problem is, although the keyPressed(KeyEvent e) method is called when you press most keys, this overriding keyPressed(KeyEvent e) method is not called when you press the tab character on the keyboard and I don’t know why not! Can anyone explain why it is not called and how can I change this program so that the panel object can respond to the tab character input? In my original un-simplified version of this program, when you press the tab character, not only does the panel fail to respond but it looses focus and focus jumps to another component in a way that I don’t know how to predict and I don’t know why that is.

public class MainClass {

    public static void main(String[] args){
          MyFrame mf = new MyFrame();
}//…end of MainClass class.

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

class MyFrame extends JFrame{

      setTitle("Frame to contain the KeyInPanel");
      KeyInPanel p = new KeyInPanel();
      Container c = getContentPane();
}//…end of MyFrame class.

import java.awt.event.*;
import java.awt.Color;
import javax.swing.JPanel;

class KeyInPanel extends JPanel {

        addKeyListener(new PanelKeyListener());
        addMouseListener(new PanelMouseListener());

    //Overrides requestFocus() method just to make the panel change
    //color so that we can see when the panel has grabbed focus
    public void requestFocus(){

     //inner class
     public class PanelKeyListener extends KeyAdapter{

       public void keyPressed(KeyEvent e) {
            short keyCode = (short) e.getKeyCode();
        if (keyCode == KeyEvent.VK_TAB) { /* Tab key*/
                   //used for debugging only
                   System.out.println("Tab key pressed");
           //...action to be performed in response to Tab key pressed
        if (keyCode == KeyEvent.VK_DELETE) { /* delete key*/
                  //used for debugging only
                  System.out.println("Delete key pressed");
        //...action to be performed in response to Delete key pressed
                //used for debugging only
System.out.println("keyCode=" + keyCode + " char=" + e.getKeyChar());
                /*…action to be performed in response to
                what ever other key is pressed…*/
     } //...end of inner class.

      //inner class
      public class PanelMouseListener extends MouseAdapter{
         //makes panal grab focus when it is clicked with the mouse
         public void mousePressed(MouseEvent e){
      }//...end of inner class.

}//...end of KeyInPanel class.


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to catch TAB key press with JScript? woojtii Javascript How-To 0 March 28th, 2007 05:53 AM
TAB KEY working together KEY PRESS event thomaz C# 4 August 20th, 2006 02:47 PM
Tab Key event ssrinivas VB How-To 5 November 4th, 2004 04:29 PM
Tab Key aliarifpk General .NET 0 February 26th, 2004 08:47 AM
Using the TAB ORDER with ENTER KEY ? thomaz C# 0 September 10th, 2003 08:10 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.