I have an app which starts with a JFrame holding 3 JButtons, one for Start, one for Quit and and one to Show the results of the work. Start obviously starts by displaying the working JFrame where the user does the required task. The starting JFrame remains visible. When the user leaves the working JFrame, the original JFrame with the 3 JButtons is left. I'd like to make it easy for the user to return to the working JFrame simply by pressing "Enter". I tried to addKeyListener to the start JButton (allowed since it is a subclass of Component), and listen for KeyEvent.VK_ENTER, but no happiness. For keyListener to work, the component needs to have the keyboard focus, so I added the requestFocus() method but no happiness. When I wrote:
startFrame.setVisible(true);
System.out.println(start.hasFocus());
System.out.println(start.isFocusTraversable());
start.requestFocus();
System.out.println(start.hasFocus());
I found on the console to my surprise the following:
false
true
false
This means that JButton doesn't get the keyboard focus? It should inherit the requestFocus method from JComponent, right? If start doesn't have the focus, what does? How do I find out? I did make the JFrame and component visible first. (I've learned that one the hard way.)
Roy
startFrame.setVisible(true);
System.out.println(start.hasFocus());
System.out.println(start.isFocusTraversable());
start.requestFocus();
System.out.println(start.hasFocus());
I found on the console to my surprise the following:
false
true
false
This means that JButton doesn't get the keyboard focus? It should inherit the requestFocus method from JComponent, right? If start doesn't have the focus, what does? How do I find out? I did make the JFrame and component visible first. (I've learned that one the hard way.)
Roy