Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

keyListener for JButton

Status
Not open for further replies.

rdoor

Technical User
May 4, 2002
43
US
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
 
The component with the focus should be apparent by the black dotted line around it.

Is it possible that the starting JFrame itself isn't the "active window" after the working frame ends? On the windows platform, you would have to click on the window to activate it in order to send keystrokes to it.

Is the working JFrame being opened as Modal?
 
Thanks for your time. I started puttering around after I got your reply. To the original code:

startFrame.setVisible(true);
System.out.println(start.hasFocus());
System.out.println(start.isFocusTraversable());
start.requestFocus();
System.out.println(start.hasFocus());

I added the following:

start.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent ke) {
if (ke.getKeyCode() == KeyEvent.VK_ENTER){
System.out.println("Enter pressed!")
}
}
});


And behold, the following!

false
true
false
Enter pressed!

Halleluiah! But it didn't have the focus! I'm happy that it worked, anyway. Any insight?

Roy
 
I wonder if there's a reason why the method is called "requestFocus" rather than "getFocus" or "transferFocus"? Maybe it gets deferred until the frame itself receives focus, or until the thread becomes inactive... if this is true then your hasFocus() test was simply occuring too quickly.

I'm glad it's working, anyway.

Ian
 
That's a good point. As I read the API, the Focus Manager actually decides and the program merely requests and lets the FM handle it as it sees best.

Thanks again!

Roy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top