milan
milan

Reputation: 2515

How do I keep keyboard focus on a single component?

I need to keep keyboard input focus on a single component inside a JPanel. It's for an application with a on-screen-keyboard.

Upvotes: 0

Views: 3999

Answers (4)

Mot
Mot

Reputation: 29600

Just a guess: take a look at the InputVerifier.

Upvotes: -1

Costis Aivalis
Costis Aivalis

Reputation: 13728

This will give you focus on your singleComponent when opening your Frame, without having to change the focus policy of enything else: singleComponent.requestFocusInWindow(); Since the focus will not freeze, you will need to setFocusable(false) for the other components like camicr suggests.

Upvotes: 0

m_vitaly
m_vitaly

Reputation: 11952

You better put the focus back to the component (component.grabFocus()) after pressing a button on the on-screen keyboard.

Or you could set focus listener (component.addFocusListener(FocusListener l)) and never let go of the focus by calling grabFocus() in the focusLost() method of FocusListener.

Upvotes: 1

camickr
camickr

Reputation: 324197

Not sure I really understand the question. But you can try something like:

otherComponents.setFocusable( false );

You also might need to use a custom FocusTraversalPolicy.

If you need more help then post a SSCCE that demonstrates the problem.

Upvotes: 1

Related Questions