Bug with KeyListener?

Create a component, make it focusable and add a key listener. then remove it from it's parent and add it back again. It doesn't respond to key events and there doesn't seem to be a way to switch this back on.

Is this a bug? If so, is it a known bug? Can anyone suggest a workaround?

package tjacobs.ui;

import java.awt.Component;

import java.awt.Container;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

publicclass Deletableimplements KeyListener{

private Component mComponent;

public Deletable(Component c){

mComponent = c;

c.addKeyListener(this);

c.setFocusable(true);

}

publicvoid keyPressed(KeyEvent ev){

if (ev.getKeyCode() == ev.VK_DELETE){

setDeleted(mComponent.getParent());

}

}

publicvoid setDeleted(Container c){

SimpleJavaBeanUndoManager man = SimpleJavaBeanUndoManager.getGlobalManager();

if (man !=null){

man.recordEvent(this,"Deleted", mComponent.getParent());

}

c.remove(mComponent);

c.invalidate();

c.validate();

c.repaint();

}

public Container getDeleted(){

return mComponent.getParent();

}

publicvoid undoDeleted(Container c){

if (c ==null){

//System.out.println("c == null!");

c = mComponent.getParent();

c.remove(mComponent);

}else{

c.add(mComponent);

mComponent.addKeyListener(this);

mComponent.setFocusable(true);

}

//mComponent.setFocusable(true);

c.invalidate();

c.validate();

c.repaint();

}

publicvoid keyReleased(KeyEvent arg0){

// TODO Auto-generated method stub

}

publicvoid keyTyped(KeyEvent arg0){

// TODO Auto-generated method stub

}

/**

* @param args

*/

publicstaticvoid main(String[] args){

//SimpleJavaBeanUndoManager man = SimpleJavaBeanUndoManager.createGlobalManager();

final JLabel l =new JLabel("Hello World");

final KeyAdapter ke =new KeyAdapter(){

publicvoid keyPressed(KeyEvent ke){

System.out.println("hit!");

}

};

l.addKeyListener(ke);

l.setFocusable(true);

new Deletable(l);

//new Draggable(l);

final JFrame f =new JFrame("Deletable");

f.add(l);

JMenuBar bar =new JMenuBar();

//JMenu undo = man.getMenu();

JMenu menu =new JMenu("Hello");

final JMenuItem item =new JMenuItem("remove");

final JMenuItem item2 =new JMenuItem("add");

ActionListener al =new ActionListener(){

publicvoid actionPerformed(ActionEvent ae){

if (ae.getSource() == item){

f.getContentPane().remove(l);

}

else{

f.getContentPane().add(l);

}

l.setFocusable(true);

l.addKeyListener(ke);

f.getContentPane().invalidate();

f.getContentPane().validate();

f.getContentPane().repaint();

}

};

item.addActionListener(al);

item2.addActionListener(al);

menu.add(item);

menu.add(item2);

bar.add(menu);

f.setJMenuBar(bar);

f.setBounds(100,100,200,200);

f.setVisible(true);

//WindowUtilities.visualize(f);

//WindowUtilities.visualize(l);

}

}

[7267 byte] By [tjacobs01a] at [2007-11-26 19:13:04]
# 1
still waiting to see if anyone knows anything about this
tjacobs01a at 2007-7-9 21:12:15 > top of Java-index,Security,Event Handling...
# 2

> still waiting to see if anyone knows anything about

> this

I don't think to many people look at this forum. At the top of this forum is this message:

"Thank you for any participation in this forum. Due to a lack of relevant activity, we have decided to archive this forum on June 16, 2006. For future posts on this topic, we suggest you use the Enterprise JavaBeans forum."

People still post here, but not many.

zadoka at 2007-7-9 21:12:15 > top of Java-index,Security,Event Handling...
# 3
> I don't think to many people look at this forum. At> the top of this forum is this message:Ah ok.Anyway I solved the issue. It was less a bug with Java and more a bug with my mind. The fix involved calling requestFocus when re-adding the label
tjacobs01a at 2007-7-9 21:12:15 > top of Java-index,Security,Event Handling...