Solved

Using enter key to traverse fields

Posted on 2000-03-09
4
245 Views
Last Modified: 2008-03-06
We would like to traverse through fields using the enter key.  Could you please give us an example on how to do this.  If I understand the javadoc stuff, I believe I need to register actions for when an enter key is pressed.

Is there an example I could look at?

Thanks.
0
Comment
Question by:holster21
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
gageot earned 25 total points
Comment Utility
You need to define a new FocusManager this way :

javax.swing.FocusManager.setCurrentManager (new MyFocusManager()) ;

this should be called once at the beginning of your program.

With FocusManager, you can define a reaction for every type of component
to every key of the keyboard. Here is an Example of FocusManager that
enables you to traverse through fields using the Enter key and activate
buttons with the Enter key :

import java.awt.Component ;
import java.awt.event.* ;

import javax.swing.* ;

public final class MyFocusManager extends DefaultFocusManager
{
public void processKeyEvent (Component aFocused, KeyEvent anEvent)
{
      if ((anEvent.getKeyCode() == KeyEvent.VK_ENTER) || anEvent.getKeyChar() == '\n')
      {
            if (anEvent.getID() == KeyEvent.KEY_PRESSED)
            {
                  if (aFocused instanceof JComponent)
                  {
                        if (((JComponent) aFocused).isManagingFocus())
                        {
                              if (0 == (anEvent.getModifiers() & ActionEvent.CTRL_MASK))
                                    return ;
                        }
                  }

                  if (aFocused instanceof AbstractButton)
                  {
                        ((AbstractButton) aFocused).doClick() ;
                  }
                  else
                  {
                        focusNextComponent (aFocused) ;
                  }
            }

            anEvent.consume() ;
      }
      else if ((anEvent.getKeyCode() == KeyEvent.VK_TAB) || anEvent.getKeyChar() == '\t')
      {
            if (anEvent.getID() == KeyEvent.KEY_PRESSED)
            {
                  if (aFocused instanceof JComponent)
                  {
                        if (((JComponent) aFocused).isManagingFocus())
                        {
                              if (0 == (anEvent.getModifiers() & ActionEvent.CTRL_MASK))
                                    return ;
                        }
                  }                        

                  if (0 != (anEvent.getModifiers() & ActionEvent.SHIFT_MASK))
                  {
                        focusPreviousComponent (aFocused) ;
                  }
                  else
                  {
                        focusNextComponent (aFocused) ;
                  }
            }

            anEvent.consume() ;
      }
}}
0
 
LVL 1

Expert Comment

by:gageot
Comment Utility
Sorry for the tabulation...
0
 
LVL 7

Expert Comment

by:vladi21
Comment Utility
use something like this:

component.registerKeyboardAction(listener, "_Enter", KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
            JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
0
 

Author Comment

by:holster21
Comment Utility
The first response was excellent! :)  You have made my day...
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Jave - Remove carriage return + special characters from text 3 62
countX 22 68
endX challenge 2 48
HashMap Vs TreeMap 12 47
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 …
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

762 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

9 Experts available now in Live!

Get 1:1 Help Now