-
KeyListener & JComponent
I'm trying to create my own custom control and want to use a KeyListener with it. But when i press a key after clicking the control i get absolutely nothing.
Any ideas?
Thanx in advance!
This is my test code...
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Test extends JComponent implements KeyListener
{
public Test()
{
this.addKeyListener(this);
}
//Key Listener
public void keyTyped(KeyEvent e){}
public void keyPressed(KeyEvent e)
{
System.out.println("Key Pressed!");
}
public void keyReleased(KeyEvent e){}
public static void main(String[] args)
{
JFrame myFrame = new JFrame("Calendar Test");
JPanel thePanel = new JPanel(new BorderLayout());
thePanel.add(new Test(), BorderLayout.CENTER);
myFrame.getContentPane().add(thePanel);
myFrame.setVisible(true);
}
}
-
ok managed to work it out!
Just so people know, you have to overide the isFocusTraversable() method and return true (or false when its disabled).
so just add this code...
Code:
public boolean isFocusTraversable()
{
return true;
}