Java: How cursor automatically move from one TextField to other
Asked Answered
R

5

7

In my application four TextArea is there and I want to enter only four character in one Text area and cursor automatically move to next TestArea. Again when I enter four character in this TextArea then again cursor automatically move to next TextArea.

Example: At the time of installing Window XP it want "Key" and there are four section when you enter four character in first section then cursor automatically move to the next section.

Same thing I want in my application.

For this first of all I add CustomizedTextFields.jar and then created four IntegerField:

private IntegerField text1;
private IntegerField text2;
private IntegerField text3;
private IntegerField text4;

after this I show all these IntegerField on my frame.

Now I tried this code to send cursor to the next field but it's not working:

text1.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                    int a2 = text1.getText().length();
                    if (a2 == 3) {
                        text2.getCursor();
                    }
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }

            @Override
            public void keyPressed(KeyEvent e) {
            }
        });
Rajasthani answered 22/3, 2012 at 9:14 Comment(1)
For better help sooner, post an SSCCE.Adventitia
E
11

interesting enough question to try improving my shadowy knowledge of the text package :-)

There are two separate requirements here

  • restrict the lenght of the text: that's done with a DocumentFilter as @mKorbel already noted
  • automatically transferFocus to the next component after the max length is reached: turns out that can be done with a NavigationFilter

in code:

JComponent panel = new JPanel();
final int maxSize = 3;
for (int i = 0; i < 4; i++) {
    final JTextField field = new JTextField(5);
    NavigationFilter filter = new NavigationFilter() {

        @Override
        public void setDot(FilterBypass fb, int dot, Bias bias) {
            if (dot >= maxSize) {
                fb.setDot(0, bias);
                field.transferFocus();
                return;
            }
            fb.setDot(dot, bias);
        }

        @Override
        public void moveDot(FilterBypass fb, int dot, Bias bias) {
            if (dot >= maxSize) { 
                fb.setDot(0, bias);
                field.transferFocus();
                return;
            }
            fb.moveDot(dot, bias);
        }

    };
    field.setNavigationFilter(filter);
    ((AbstractDocument) field.getDocument()).setDocumentFilter(new DocumentSizeFilter(maxSize));
    panel.add(field);
}

The documentFilter is the one from the Swing Tutorial

Equiangular answered 22/3, 2012 at 11:39 Comment(5)
@Vinit Vikash and this is answer to your questionStreamer
@Equiangular : +1 for NavigationFilter. Actually , till today, this term was not in my knowledge :-) . Still reading about this, feels like what you had told is the way to go, since I was hoping that someone can put more light on this, that's why I posted the answer, else I never wanted to.Histone
But, when I applied transferFocus() inside NavigationFilter, nothing changed, so If you may, post the whole code, I shall be really obliged.Histone
@GagandeepBali that is the whole code - simply throw it into your favourite frame and run it :-)Equiangular
Now it's working, LOL, last time it gave me all sorts of wicked errors. Just one thing, change the maxSize to 4, as OP wants for 4 Characters. Deleting my answer :-)Histone
S
6
At the time of installing Window XP it want "Key" and there are four section 
when you enter four character in first section then cursor automatically move 
to the next section.
  1. add DocumentListener to the JTextComponents, for listening add DocumentFilter

  2. don't use KeyListener for JTextComponents, use only DocumentListener

  3. add required next JTextArea to the DocumentListener, if is there typed 4th. Char into JTextArea,

  4. notice, moving with Focus from one JTextArea to another would be better wrapped into invokeLater

Streamer answered 22/3, 2012 at 9:20 Comment(0)
K
1

Replace text2.getCursor() with text2.requestFocus().

getCursor() is for retrieving the shape of the mouse pointer when hovering over a component.

Also, with this method it is still possible to enter more than 4 chars in a field, for example by pasting from clipboard. If you want to block that, you would need to check if text entered is longer than 4 chars, and if so, take only first 4 chars from it.

Kurus answered 22/3, 2012 at 9:18 Comment(0)
A
1

Something like this should work:

text1.addKeyListener(new KeyAdapter(){
          public void keyPressed(KeyEvent e){
             String value=text1.getText();
             if(value.length()==4){
             text2.requestFocus();
          }
}

Where text2 is your next textfield

Aklog answered 22/3, 2012 at 9:19 Comment(1)
don't use keyListeners at all ... especially not for validating text input, instead see @mKorbel's answerEquiangular
Y
0

simply just create textarea and go to key typed events den u may write this

String number=jTextArea1.getText();
 int l=number.length();
 if(l==3){
 jTextArea1.transferFocus();

 }
Yuzik answered 20/2, 2018 at 7:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.