Solved

Java Set Focus CardLayout

Posted on 2014-12-18
5
212 Views
Last Modified: 2014-12-22
In the example here:
http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html
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
cards.requestFocus();

Open in new window

with no effect.
0
Comment
Question by:deleyd
  • 2
  • 2
5 Comments
 
LVL 16

Expert Comment

by:krakatoa
ID: 40508758
You'd have to create a new JTextField

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

Open in new window


and then call requestFocus() on it.
0
 
LVL 37

Accepted Solution

by:
zzynx earned 500 total points
ID: 40509349
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.
0
 

Author Comment

by:deleyd
ID: 40509612
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?)
0
 

Author Closing Comment

by:deleyd
ID: 40510306
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:

http://tips4java.wordpress.com/2008/10/31/card-layout-focus/
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40512819
Thanx 4 axxepting
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question