Reputation: 16555
I have one button which set time on Timer. So now I push. Button change time on Timer and this is still selected. In this frame I have keyListener so when I push space Timer with start but now when I push space it push again this button because it is still selected. How I can improve this button when I push it, it will not be selected ?
Upvotes: 2
Views: 6651
Reputation: 13153
You can change the focus by calling the correct method in KeyboardFocusManager
KeyboardFocusManager kfm = KeyboardFocusManger.getCurrentKeyboardFocusManager();
kfm.focusNextComponent();
This causes focus to move to the next component, whatever it is. This has the advantage of being independent of what the component is, so that if the UI changes, this still moves to the "next" component rather than a specific component you specify to receive focus.
If your problem is that you do not want the user to press the button while other things are happening, you should consider disabling the button (as explained in a previous answer) so that it cannot be activated in any way. You need to enable it again, of course, as soon as it is legal to use it again.
rc
Upvotes: 0
Reputation: 39204
I'm not sure of having fully understood your question. If you want to disable click on a JButton:
JButton b = new JButton();
b.setEnabled(false);
If you want to unselect it:
b.setSelected(false);
You could also find useful prevent a button to gain focus:
b.setFocusable(false);
Upvotes: 6