Solved

Java Set Focus CardLayout

Posted on 2014-12-18
5
223 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

726 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