Solved

Using enter key to traverse fields

Posted on 2000-03-09
4
248 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
[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
4 Comments
 
LVL 1

Accepted Solution

by:
gageot earned 25 total points
ID: 2601372
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
ID: 2601375
Sorry for the tabulation...
0
 
LVL 7

Expert Comment

by:vladi21
ID: 2601377
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
ID: 2601585
The first response was excellent! :)  You have made my day...
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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 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…
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:

726 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