3

In my Java Swing application I am using a JToolBar with several buttons. After the toolbar object is being created I call setFocusable(false). However, every time I start the application the first button in the toolbar is focused.

Any ideas how I can prevent the toolbar buttons to come into focus at all?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Matthias
  • 9,817
  • 14
  • 66
  • 125

1 Answers1

1

Firstly, Toolbar buttons should not have

setFocusable(false)

called on them, as it breaks keyboard support. Rather, call:

setRequestFocusEnabled(false)

which will stop them from taking over the focus when the mouse clicks them, but still lets keyboard-centric users "tab over" to them.

You probably want to select some other sensible component to place the initial keyboard focus onto. So just call:

otherComponent.requestFocusInWindow();
Luke Usherwood
  • 3,082
  • 1
  • 28
  • 35