keyListener on a JDialog

Hi all,

I am trying to add a key listener to a JDialog. Actually, this code used to worke well with SDK earlier than 1.6.

When I run the application the JDialog shows up and the key listeners works fine as long as I do nothing. But as soon as I move the dialog, or if its content changes (there is an image in the background that changes periodically), the listener stops working.

Here is the (simpliefied) code:

publicclass Problemextends JDialog{

privatestatic JPanel panel =null;

public CamViewer(String url){

this.addKeyListener(new KeyListener(){

publicvoid keyPressed(KeyEvent e){

doSomething(e);

}

publicvoid keyTyped(KeyEvent e){

}

publicvoid keyReleased(KeyEvent e){

}

});

panel =new JPanel(){

publicvoid paintComponent(Graphics g){

g.drawImage(getIcon().getImage(), 0, 0,null);

setOpaque(false );

super.paintComponent(g);

}

};

scrollPane =new JScrollPane( panel );

setContentPane( scrollPane );

}

void doSomething(KeyEvent ke){

// do some work

}

publicstaticvoid main(String [] args){

Problem frame =new Problem();

frame.setVisible(true);

}

}

Does anyone have an idea were am I wrong? Thanks!

[2747 byte] By [tsktska] at [2007-11-26 19:58:32]
# 1
KeyEvents are passed to the component that has focus. So you should probably be making the panel focusable and then adding the KeyListener to the panel, not the dialog.
camickra at 2007-7-9 22:54:21 > top of Java-index,Desktop,Core GUI APIs...
# 2
thank you, I'll try that!
tsktska at 2007-7-9 22:54:21 > top of Java-index,Desktop,Core GUI APIs...