?
Solved

Using enter key to traverse fields

Posted on 2000-03-09
4
Medium Priority
?
253 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 100 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

777 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