[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2009-04-25
19
Medium Priority
?
1,231 Views
Last Modified: 2012-05-06
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!
0
Comment
Question by:duta
  • 10
  • 4
  • 4
  • +1
19 Comments
 
LVL 12

Expert Comment

by:basav_com
ID: 24232590
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 24232608
Add a key mapping for the enter key and call requestFocus on the next field in actionPerformed
0
 

Author Comment

by:duta
ID: 24232610
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:duta
ID: 24232615
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 24232633
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 24232640
>>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
 

Author Comment

by:duta
ID: 24232699
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
 

Author Comment

by:duta
ID: 24232777
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 24232854
Sorry i meant insertUpdate, not insertString. Here's an example

http://www.exampledepot.com/egs/javax.swing.text/ChangeEvt.html
0
 
LVL 92

Expert Comment

by:objects
ID: 24233614
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
 

Author Comment

by:duta
ID: 24233645
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
 
LVL 92

Expert Comment

by:objects
ID: 24234117
you are missing imports for DocumentListener and DocumentEvent

import javax.swing.event.*;
 

0
 

Author Comment

by:duta
ID: 24234196
: 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
 

Author Comment

by:duta
ID: 24234783
Savant, and Genius, the error  seems to be with  "+ccurentIx % myTxt.

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



Thanks!
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 24234800
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
 

Author Comment

by:duta
ID: 24234839
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
 
LVL 92

Expert Comment

by:objects
ID: 24234843
can you post you r current code inclusing where myText is declared and created

0
 

Author Closing Comment

by:duta
ID: 31574534
You are great!
0
 

Author Comment

by:duta
ID: 24234907
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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 Month18 days, 10 hours left to enroll

834 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