DVK
DVK

Reputation: 129433

How do I lose focus on a JComboBox?

I have a JComboBox with an key listener.

When I hit <enter>, I fire off some action, and then I need to to lose focus on the JComboBox!

To focus on it, I can do JComboBoxObject.grabFocus();

But doing transferFocus() to get the focus to a next element (I don't care WHERE the focus goes, just away from combo box) does NOT work.

Doing grabFocus() from another combo box works, but seems like a pretty annoying hack to me. Is there a better solution?

Upvotes: 1

Views: 4056

Answers (2)

MMujtabaRoohani
MMujtabaRoohani

Reputation: 483

I can suggest you to first use the

.getNextFocusableComponent()

and then use the

.requestFocusInWindow()

that means Implementing it like this,

JComboBox.getNextFocusableComponent().requestFocusInWindow();

One important note is that .getNextFocusableComponent() has become obsolete but it can work really better, you can use it but If you have any other solution, I would prefer not using this.

Upvotes: 3

trashgod
trashgod

Reputation: 205805

Updated: Starting from this two-combo example, adding either of these lines to the actionPerformed() implementation seems to do what you want.

combo1.transferFocus();
combo2.requestFocusInWindow();

Upvotes: 3

Related Questions