Add/Remove/Replace KeyboardFocusManager
Hi,
There are many buttons in a Applet and on clicking on each button an instance of another applet is opened. This Applet uses AWT and I am handling events for all the keys and I am using KeyboardFocusManager. When multiple instances of the applet is opened and when TAB key is pressed, the action goes to the first instance of the applet opened. So I am adding KeyboardFocusManager on FocusGained and removing it on FocusLost. But I am getting a SecurityException, when I am adding and removing KeyboardFocusManager:
java.lang.SecurityException: this KeyboardFocusManager is not installed in the current thread's context
at java.awt.KeyboardFocusManager.getGlobalFocusedWindowat java.awt.DefaultKeyboardFocusManager.dispatchEvent
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.SequencedEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy
at java.awt.EventDispatchThread.pumpEventsForHierarchy
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Following is my code on FocusGained and FocusLost:
public void FocusGained(FocusEvent e) {
DefaultKeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
int step = 1;
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_TAB) {
step = (++step) % 2;
if (step == 0) {
write(e.getKeyChar());
}
e.consume();
return true;
}
return false;
}
});
}
public void FocusLost(FocusEvent e){
KeyboardFocusManager.getCurrentKeyboardFocusManager().setCurrentKeyboardFocusManager(null);
}
Any thoughts how to replace the KeyboardFocusManager ?
Thanks

