KeyListener does not work...plz help

i have created an application based on a tutorial that will keep drawing rectangles until any key is pressed, then it will continue when i press again

the problem is that the "KeyListener" does not respond and the application keep drawing without stop

my code is:

publicclass simpleLoopextends JPanelimplements KeyListener, MouseListener{

Thread t;

Random rand =new Random();

boolean e =false;

/** Creates a new instance of simpleLoop */

public simpleLoop(){

setFocusable(true);

requestFocus();

addKeyListener(this);

addMouseListener(this);

t = creatThread();

t.start();

}

private Thread creatThread(){

returnnew Thread(new Runnable(){

publicvoid run(){

while(!e){

try{

Thread.sleep(20);

}catch (InterruptedException ex){

ex.printStackTrace();

}

repaint();

}

}

});

}

protectedvoid paintComponent(Graphics g){

Graphics2D g2d = (Graphics2D)g;

int x;

int y;

int hight,width;

int colorR,colorG,colorB;

//AffineTransform identity = new AffineTransform();

x = rand.nextInt(getWidth());

y = rand.nextInt(getHeight());

hight = rand.nextInt(getHeight()/5);

width = rand.nextInt(getWidth()/5);

Rectangle rec =new Rectangle(x,y,hight,width);

colorB = rand.nextInt(256);

colorG = rand.nextInt(256);

colorR = rand.nextInt(256);

g2d.setColor(new Color(colorR,colorG,colorB));

g2d.fill(rec);

}

publicstaticvoid main(String[] arg){

JFrame frame =new JFrame("simple loop");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

frame.setSize(400,400);

frame.setFocusable(true);

frame.requestFocus();

simpleLoop sl =new simpleLoop();

frame.getContentPane().add(sl,BorderLayout.CENTER);

frame.setResizable(false);

frame.setLocationRelativeTo(null);

}

publicvoid keyTyped(KeyEvent e){

System.out.println("pressed type");

}

publicvoid keyPressed(KeyEvent e){

System.out.println("pressed press");

if(this.e ==false){

this.e =true;

}else{

this.e =false;

t = creatThread();

}

}

publicvoid keyReleased(KeyEvent e){

System.out.println("pressed release");

}

publicvoid mouseClicked(MouseEvent e){

}

publicvoid mousePressed(MouseEvent e){

System.out.println("clicked");

repaint();

}

publicvoid mouseReleased(MouseEvent e){

}

publicvoid mouseEntered(MouseEvent e){

}

publicvoid mouseExited(MouseEvent e){

}

}

i am using netbeans and windows xp

NOTE:

i don't know why but the KeyListener does work fine with another application that i have also created it based on another tutorial

publicclass KeyListenerExampleextends JFrame{

DrawPanel dp =new DrawPanel();

/** Creates a new instance of DrawingExample */

public KeyListenerExample(){

super("Simple Drawing Example");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

add(dp,BorderLayout.CENTER);

setSize(400,300);

setVisible(true);

}

publicstaticvoid main(String [] args){

new KeyListenerExample();

}

}

class DrawPanelextends JPanel{

privateint keyCode;

privatechar keyChar;

public DrawPanel(){

setFocusable(true);

requestFocus();

addKeyListener(new KeyListener(){

publicvoid keyPressed(KeyEvent e){

keyCode = e.getKeyCode();

keyChar =' ';

repaint();

}

publicvoid keyReleased(KeyEvent e){

}

publicvoid keyTyped(KeyEvent e){

keyChar = e.getKeyChar();

repaint();

}

});

}

protectedvoid paintComponent(Graphics g){

super.paintComponent(g);

g.drawString("press a key...",20,20);

g.drawString("Key Code: "+keyCode,20,50);

g.drawString("Key Char:"+keyChar,20,70);

}

}

plz help....

[8920 byte] By [Lich_Kinga] at [2007-11-26 23:46:00]
# 1
When I run your sample code, paintComponent is never called and I do not see any rectangles. Can you correct your sample code or are you sure it is working for you? Thanks.
pthorsona at 2007-7-11 15:18:44 > top of Java-index,Desktop,Core GUI APIs...
# 2
Nevermind. I moved set visible to the end of the main class and now I see your issue. I'll see what I can figure out.
pthorsona at 2007-7-11 15:18:44 > top of Java-index,Desktop,Core GUI APIs...
# 3

If you add the keylistener to the frame it seems to work:

frame.addKeyListener(sl);

I wonder if the focus request is getting ignored. You might try setting focus as the last step in main.

Note also, that we probably should not be updating the UI from anything other the event dispatch thread, but that does not seem to be the issue here.

pthorsona at 2007-7-11 15:18:44 > top of Java-index,Desktop,Core GUI APIs...
# 4
> i don't know why but the KeyListener does work fine with another application that i have also created Well then compare the code and see what different.For example when do you add the component to the frame. When do you set the frame visible.
camickra at 2007-7-11 15:18:44 > top of Java-index,Desktop,Core GUI APIs...
# 5
thnx pthorson....i did as you said and it workedwhat bothers me is that my friend did the same application and it worked fine with himi took his code and tried it on my computer but no good .... the keyListener wont respondwhat is the meaning of this?....
Lich_Kinga at 2007-7-11 15:18:44 > top of Java-index,Desktop,Core GUI APIs...