How do I use a Tab key to maximize a frame?

How can I use a Tab key (vk_tab) pressed on the focused frame to actually maximize the frame... what action should it have attached to it?thanks!
[159 byte] By [rejavaa] at [2007-11-27 4:12:39]
# 1
setExtendedState(...);
camickra at 2007-7-12 9:18:40 > top of Java-index,Desktop,Core GUI APIs...
# 2

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);

}

}

});

rejavaa at 2007-7-12 9:18:40 > top of Java-index,Desktop,Core GUI APIs...
# 3

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;

}

});

Michael_Dunna at 2007-7-12 9:18:40 > top of Java-index,Desktop,Core GUI APIs...
# 4

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.

camickra at 2007-7-12 9:18:40 > top of Java-index,Desktop,Core GUI APIs...