Java Set Focus CardLayout

In the example here:
How would I modify it so when we switch to the JTextField card, the JTextField has focus? Currently the ComboBox still has focus after switching to the JTextField card.

I've tried adding

Open in new window

with no effect.
Who is Participating?
zzynxConnect With a Mentor Software engineerCommented:
How would I modify it so when we switch to the JTextField card, the JTextField has focus?
by calling requestFocusInWindow() upon that JTextField instance when the tab selection is changed.

Remark: @ the documentation of the requestFocus() method I read:
Because the focus behavior of this method is platform-dependent, developers are strongly encouraged to use requestFocusInWindow when possible.
You'd have to create a new JTextField

JTextField jtf = new JTextField("TextField",20);

Open in new window

and then call requestFocus() on it.
deleydAuthor Commented:
How can I determine which card (JPanel) is currently being shown?

Does KeyboardManager keep track of what component had focus for each card? Or do I need to do that manually? (So when I switch between cards, the component that last had focus on whichever card I switch to still has focus.)

Otherwise if Keyboard Manager doesn't keep track of which component has focus in all my JPanels, then I have to store that information myself, and retrieve it and set the focused component every time a JPanel is displayed. (In which case, what is the point of Keyboard Manager, if I have to do it manually anyway?)
deleydAuthor Commented:
I found another page claiming drawback of CardLayout is:

focus is not placed on the Card as it is displayed
there is no API to determine which Card is currently being displayed

So they extended CardLayout to add those features:
zzynxSoftware engineerCommented:
Thanx 4 axxepting
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.