0

Possible Duplicate:
ENTER key using in JTextField java

Hi i have 4 JTextFields in my swing form. After entering certain values in JTextField1 and i hit the enter in the keyboard now the focus needs to transfer from JTextfield1 to JTextField2 after entering certain value in JTextField2 and hit enter the focus needs to transfer to JTextField3 in this way i don't know how to do this the code i have used was

package focus;
import java.awt.FlowLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;

public class Main{

  public static void main(String[] argv) throws Exception {

    JTextField component = new JTextField(20);
    JTextField component1 = new JTextField(20);
    JTextField component2 = new JTextField(20);
    JTextField component3 = new JTextField(20);
    KeyListener kl=new KeyListener() {

            public void keyTyped(KeyEvent e) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            public void keyPressed(KeyEvent e) {
                throw new UnsupportedOperationException("Not supported yet.");
                int key = e.getKeyCode();
            if (key == KeyEvent.VK_ENTER)
              component.transferFocus();
            }

            public void keyReleased(KeyEvent e) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        };
    component.addFocusListener(new MyFocusListener<JTextField>());
    component1.addFocusListener(new MyFocusListener<JTextField>());
    component2.addFocusListener(new MyFocusListener<JTextField>());
    component3.addFocusListener(new MyFocusListener<JTextField>());
        JFrame f = new JFrame();
    f.setLayout(new FlowLayout());
    f.add(component1);
    f.add(component);
     f.add(component2);
      f.add(component3);
    f.pack();
    f.setVisible(true);

  }
}
class MyFocusListener <C extends JTextField> extends  FocusAdapter {
  //boolean showingDialog = false;
  public void focusGained(FocusEvent evt) {
    final JTextComponent c = (JTextComponent) evt.getSource();
    String s = c.getText();
    c.requestFocus();
    c.selectAll();
    for (int i = 0; i < s.length(); i++) {
   if (!Character.isDigit(s.charAt(i))) {
    c.setSelectionStart(i);
        c.setSelectionEnd(i);
        break;
    }
    }
  }

  public void focusLost(FocusEvent evt) {
    final JTextComponent c = (JTextComponent) evt.getSource();
    String s = c.getText();
   if (evt.isTemporary()) {
     return;
    }
    for (int i = 0; i < s.length(); i++) {
     if (!Character.isDigit(s.charAt(i))) {
        //System.out.println("must only contain digits");
        c.requestFocus();
        c.selectAll();
        break;
      }
    }

}
}
Community
  • 1
  • 1

1 Answers1

0

Simply add ActionListeners to the text fields and from the ActionEvent you get the object. Then use the transferFocus() method to transfer the focus to other textfield.

Mohsin
  • 852
  • 8
  • 28