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();
mf.setVisible(true);
}
}//â¦end of MainClass class.
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class MyFrame extends JFrame{
//consructor
MyFrame(){
setTitle("Frame to contain the KeyInPanel");
setSize(500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
KeyInPanel p = new KeyInPanel();
Container c = getContentPane();
c.setBackground(Color.WHITE);
c.add(p);
}
}//â¦end of MyFrame class.
import java.awt.event.*;
import java.awt.Color;
import javax.swing.JPanel;
class KeyInPanel extends JPanel {
//constructor
KeyInPanel(){
setBackground(Color.GREEN);
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(){
super.requestFocus();
setBackground(Color.BLUE);
}
//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
return;
}
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
return;
}
//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){
requestFocus();
}
}//...end of inner class.
}//...end of KeyInPanel class.
:)AndrewH
|