Solved

Focus Traversing with user action "ENTER"

Posted on 2009-04-15
3
408 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 200 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 50 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
printf performancy 11 101
java imports not found 4 71
Selenium docs api java index 3 96
spring maven example issues 3 97
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

739 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