
how to move curosr from one text field to another automatically when the first textfield reaches its maximum length

Hi Friend,
Here we have created a program where if the user exceeds the limit of six characters in the first text filed, then the cursor automatically moved to the second one.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MoveCursor{
public static void main(String[]args){
JLabel lab1=new JLabel("Name");
JLabel lab2=new JLabel("Address");
final JTextField text1=new JTextField(20);
final JTextField text2=new JTextField(20);
JPanel p=new JPanel(new GridLayout(2,2));
JFrame f=new JFrame();
p.add(lab1);
p.add(text1);
p.add(lab2);
p.add(text2);
f.add(p);
f.setVisible(true);
f.pack();
text1.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
String value=text1.getText();
if(value.length()==5){
text2.requestFocus();
}
}
});
}
}
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.