Solved

Using enter key to traverse fields

Posted on 2000-03-09
4
250 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 …
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:

717 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