Solved

Java Set Focus CardLayout

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
map related example 6 49
reverse digits of a number using for loop 5 45
Error in @AspectJ Based AOP with Spring 2 13
eclipse console opening separately 2 21
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

821 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