Solved

Java Set Focus CardLayout

Posted on 2014-12-18
5
194 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now