If I press Tab key, JFrame does not work. Why?
final JFrame frame = new JFrame("ScrollDemo2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_TAB) {
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
}
});
tab is the focus traversal key, so will be consumed before getting to your listener
try this instead
KeyboardFocusManager.getCurrentKeyboardFocusManager()
.addKeyEventDispatcher(new KeyEventDispatcher(){
public boolean dispatchKeyEvent(KeyEvent e){
if(e.getID() == KeyEvent.KEY_PRESSED)
{
if(e.getKeyCode() == KeyEvent.VK_TAB ) f.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
return false;
}
});
Probably because the frame doesn't have focus. Some component on the frame has focus.
Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html]How to Use Key Bindings[/url] for a better approach.
Or, even easier would be to create a menu with a menu item titled "Maximize Frame" then you can assign an accelerator to the menu item.