JAVA: How to move the cursor to the next TextField at the click of "ENTER" key?

Dear Experts!

Hi!

I have 10 TextFields into which the user is required to enter a letter.

I thought it would be very convenient if  the cursor is moved to the next TextField when he pressed "ENTER" key.

Thanks a lot!
dutaAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
try this:

if     ( int = i, i < 10; i++   )
{
        myTxt [i ] =  new  JTextField ( 1 );
         if (i<9)
        myTxt [ i ].getDocument().addDocumentListener(new MyDocumentListener(myText[i+1]));

}

 class MyDocumentListener implements DocumentListener
 {
   JComponent next = null;

   public  MyDocumentListener(JComponent next)
   {
       this.next = next;
   }
     
public  void      changedUpdate(DocumentEvent e)  { moveCursor(e); }
public  void      insertUpdate(DocumentEvent e)  { moveCursor(e); }
public  void      removeUpdate(DocumentEvent e)  { moveCursor(e); }

       public void moveCursor(DocumentEvent e)
       {
          if (e.getDocument().getLength()==1)
          next.requestFocusInWindow();
       }
   
    }

0
 
basav_comCommented:
In General movement thru the items is by using :
Forward : 'Tab key'
Backward : 'Shift + Tab key'

That is how everyone knows...and prefers. Is your requirement is different ?
0
 
CEHJCommented:
Add a key mapping for the enter key and call requestFocus on the next field in actionPerformed
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
dutaAuthor Commented:
Thank you so much for your super-prompt reply.
I was about to change my question because I wanted something else.

What I wanted is to:

I have 1- textfields which is to be filled with one character.
When a user types in a character, the cursor moves to the next textfield even without pressing "Tab" key
or "Enter" key.

I think that is impossible because the  char is read by the system when either "Tab" or "Enter" is not pressed.

Am I correct?
Thanks!

0
 
dutaAuthor Commented:
CORRECTION:

ORIGINAL:   have 1- textfields which is to be filled with one character.


MODIFIED:  I  have 10   Textfields which are  to be filled with one character.
0
 
CEHJCommented:
See http://www.exampledepot.com/egs/javax.swing/KeyStrokes.html and related

Your actionPerformed would be something like
textFields[++currentIx % textFields.length].requestFocus(); 

Open in new window

0
 
CEHJCommented:
>>When a user types in a character, the cursor moves to the next textfield even without pressing "Tab" key

Add a DocumentListener to the Document of your text fields and move forward in insertString
0
 
dutaAuthor Commented:
What I need to do is:

1.   Add Document Listener to each of my 10 TextFields

2.  Create a Document Listener class

3   Create actions to be performed in the Listener class.

Am I right?

It is quite a daunting   task for a beginner  :)

Thanks!
0
 
dutaAuthor Commented:
I use the following code. Obviously it is incomplete.  Can you kindly help me fix it?

Do I need to import certain classes to use the DocumentListener?

Thanks a lot!


________________________________________________

JTextField [  ]  MyTxt =  new   TextField (  10  )


if     ( int = i, i < 10; i++   )
{
        myTxt [i ] =  new  JTextField ( 1 );

        myTxt [ i ].addActionListener(new MyDocumentListener());

}

_________________________________

 class MyDocumentListener implements MyDocumentListener
    {        
 
       public void moveCursor(DocumentEvent e)
       {
          myTxt  [++currentIx %myTxt.length].requestFocus();
       }
   
    }
_____________________________________________________________
_________________________________________________
0
 
CEHJCommented:
Sorry i meant insertUpdate, not insertString. Here's an example

http://www.exampledepot.com/egs/javax.swing.text/ChangeEvt.html
0
 
objectsCommented:
that not right, you need something like this:

MyDocumentListener  listener = new MyDocumentListener ();
if     ( int = i, i < 10; i++   )
{
        myTxt [i ] =  new  JTextField ( 1 );

        myTxt [ i ].getDocument().addDocumentListener(new MyDocumentListener());

}

_________________________________

 class MyDocumentListener implements DocumentListener
    {        
public  void      changedUpdate(DocumentEvent e)  { moveCursor(e); }
public  void      insertUpdate(DocumentEvent e)  { moveCursor(e); }
public  void      removeUpdate(DocumentEvent e)  { moveCursor(e); }

       public void moveCursor(DocumentEvent e)
       {
          if (e.getDocument().getLength()==1)
          myTxt  [++currentIx %myTxt.length].requestFocusInWindow();
       }
   
    }

0
 
dutaAuthor Commented:
Savant, thank you so much for your kind, extraordinary help.

When I tried your tip, I got some errors.

Some of the message are:
_________________________________________________________________

1.     javax.swing.text.Document cannot be applied to (Lottery.MyDocumentListener)

2      java:182: cannot find symbol  symbol  : class DocumentListener

3.     java:189: cannot find symbol symbol  : class DocumentEvent
_____________________________________________________________________

Thanks a lot!
0
 
objectsCommented:
you are missing imports for DocumentListener and DocumentEvent

import javax.swing.event.*;
 

0
 
dutaAuthor Commented:
: cannot find symbol  symbol  : variable currentIndex

Savant, thank you so much for your very patient, kind help.

I got the following message when I last tried:

 cannot find symbol       symbol  : variable currentIndex

_______________ The code I used _________________

 class MyDocumentListener implements DocumentListener
    {        
public  void      changedUpdate(DocumentEvent e)  { moveCursor(e); }
public  void      insertUpdate(DocumentEvent e)  { moveCursor(e); }
public  void      removeUpdate(DocumentEvent e)  { moveCursor(e); }

       public void moveCursor(DocumentEvent e)
       {
          if (e.getDocument().getLength()==1)
          myTxt  [++currentIx %myTxt.length].requestFocusInWindow();
       }
   
    }
_________________________________________________
0
 
dutaAuthor Commented:
Savant, and Genius, the error  seems to be with  "+ccurentIx % myTxt.

The error message says: " cannot find symbol: variable currentIx"



Thanks!
0
 
dutaAuthor Commented:
I think we are almost there.

I got the error message as the follows:

________________  Error Message ________________
java.lang.ArrayIndexOutOfBoundsException: 10
      at Lottery.buildPanel(Lottery.java:150)
      at Lottery.<init>(Lottery.java:66)
      at Lottery.main(Lottery.java:303)
_______________________________________________

I think  the cursor should stop moving when it reached the last (10th) textfield.


Thanks a lot!

0
 
objectsCommented:
can you post you r current code inclusing where myText is declared and created

0
 
dutaAuthor Commented:
You are great!
0
 
dutaAuthor Commented:
Savant, your code is perfect.

The only modification that I made is to change 10 to 9 in order to avoid the dreadful
"ArrayIndexOutOfBoundsException".

Thanks a lot!

___________________________________________

if     ( int = i, i < (10-1); i++   )
{
        myTxt [i ] =  new  JTextField ( 1 );
         if (i<9)
        myTxt [ i ].getDocument().addDocumentListener(new MyDocumentListener(myText[i+1]));
___________________________________________________

}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.