?
Solved

Focus Traversing with user action "ENTER"

Posted on 2009-04-15
3
Medium Priority
?
415 Views
Last Modified: 2012-05-06
Hi,

I am trying to find a way to allow users keying in fields by traversing via TAB key or ENTER key from keyboard. TAB working fine from fields to fields but users need to have ENTER for traversing as well. I found a good reference here in EE but some of the methods have deprecated.

I found these from other resources, but it claims to be 'unchecked or unsafe  operations'.
<code>
Set set = new HashSet(
        KeyboardFocusManager.getCurrentKeyboardFocusManager().getDefaultFocusTraversalKeys(
            KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));
    set.clear();   // Call clear() if you want to eliminate the current key set
    set.add(KeyStroke.getKeyStroke("ENTER"));
    KeyboardFocusManager.getCurrentKeyboardFocusManager().setDefaultFocusTraversalKeys(
        KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set);
</code>

I try to apply above for my code but I wasn't sure if its working and how should I be traverse from 1 field to another. Any advice is appreciated. Thanks.
0
Comment
Question by:ai_chien
[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
3 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 24146147
0
 
LVL 26

Accepted Solution

by:
ksivananth earned 800 total points
ID: 24146154
for e.g.,

Set forwardKeys = txtField.getFocusTraversalKeys(
    KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
Set newForwardKeys = new HashSet(forwardKeys);
newForwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
txtField.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
    newForwardKeys);

0
 
LVL 17

Assisted Solution

by:Thomas4019
Thomas4019 earned 200 total points
ID: 24153651
Java now has generics as of Java 1.5
That means you should no longer declare a "Set someSet" like that but now you must specify its type. The following is if the set contains Strings "Set someSet".

Set<AWTKeyStrole> set = new HashSet<AWTKeyStrole>(
instead of
Set set = new HashSet(



Set<AWTKeyStrole> set = new HashSet<AWTKeyStrole>(
        KeyboardFocusManager.getCurrentKeyboardFocusManager().getDefaultFocusTraversalKeys(
            KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));
    set.clear();   // Call clear() if you want to eliminate the current key set
    set.add(KeyStroke.getKeyStroke("ENTER"));
    KeyboardFocusManager.getCurrentKeyboardFocusManager().setDefaultFocusTraversalKeys(
        KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set);

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month10 days, 12 hours left to enroll

765 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