Solved

Using enter key to traverse fields

Posted on 2000-03-09
4
247 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
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Checkbox and ListView in Android Layout 4 65
GUI builder for Eclipse? 8 35
javap not working 8 44
jsp login check 12 31
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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.

770 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