Solved

Focus Traversing with user action "ENTER"

Posted on 2009-04-15
3
376 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
  • 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

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

706 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

18 Experts available now in Live!

Get 1:1 Help Now