Reputation: 18364
I want to make a Java swing button 'not-focussable'. The button should not receive focus at all, but should be able to receive mouse clicks.
I thought the following options, but these either dont solve my problem completely, or dont seem elegant. Are there any other/better/suggested options?
Upvotes: 2
Views: 4497
Reputation: 93197
Did you try to call the setFocusable()
method inherited from java.awt.Component
?
Resources :
Upvotes: 2
Reputation: 52478
All Swing components have a setFocusable method to do this:
JButton button = ...
button.setFocusable(false);
Upvotes: 14
Reputation: 36987
You can implement your own FocusTraversalPolicy (or extend e.g. ContainerOrderFocusTraversalPolicy) with an accept method that just doesn't like your button.
JFrame frame = new JFrame();
... /* create other components */
frame.setFocusTraversalPolicy(new ContainerOrderFocusTraversalPolicy() {
public boolean accept(Component c) {
return super.accept(c) && c!=iDontLikeYouButton;
}
});
Upvotes: 1