Java: how the cursor automatically moves from one text field to another

My application has four TextArea, and I want to enter only four characters in one area of ​​the text, and the cursor will automatically move to the next TestArea. Again, when I enter four characters into this TextArea, then again the cursor will automatically move to the next TextArea.

Example: during the installation of Window XP, it wants β€œKey”, and there are four sections when you enter four characters in the first section, and then the cursor automatically moves to the next section.

The same thing that I want in my application.

To do this, first add CustomizedTextFields.jar and then create four IntegerField:

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

after that I show all these IntegerField on my frame.

Now I tried this code to send the cursor to the following field, but it does not work:

 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) { } }); 
+7
source share
6 answers

interesting enough question to try to improve my shadow knowledge of a text package :-)

There are two separate requirements.

  • limit text length: this is done using DocumentFilter as already noted by @mKorbel
  • automatically transfers Focus to the next component after reaching its maximum length: it turns out that this can be done using 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); } 

DocumentFilter is the one from the Swing Tutorial

+10
source
 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. 
  • add DocumentListener to JTextComponents , to listen add DocumentFilter

  • do not use KeyListener for JTextComponents , use only DocumentListener

  • add the following JTextArea to the DocumentListener if you typed 4. Char in JTextArea ,

  • moving with Focus from one JTextArea to another would be better wrapped in invokeLater

+6
source
 import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JTextField; public class NextBox extends JTextField { private JComponent nextComponent; public NextBox() { addKeyListener(new KeyListener() { @Override public void keyTyped(KeyEvent e) {} @Override public void keyReleased(KeyEvent e) { // Check for the four characters. if (getText().length() >= 4) { nextComponent.requestFocus(); } } @Override public void keyPressed(KeyEvent e) {} }); } public static void main(String[] args) { JFrame frame = new JFrame("Debug Frame"); frame.setSize(400, 80); frame.setLayout(new GridLayout(1,5)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); NextBox box1 = new NextBox(); NextBox box2 = new NextBox(); NextBox box3 = new NextBox(); NextBox box4 = new NextBox(); JButton button1 = new JButton("Done"); box1.setNextComponent(box2); box2.setNextComponent(box3); box3.setNextComponent(box4); box4.setNextComponent(button1); frame.add(box1); frame.add(box2); frame.add(box3); frame.add(box4); frame.add(button1); frame.setVisible(true); } /** * @param nextComponent the nextComponent to set */ public void setNextComponent(JComponent nextComponent) { this.nextComponent = nextComponent; } /** * @return the nextComponent */ public JComponent getNextComponent() { return nextComponent; } } 
+2
source

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

getCursor() designed to get the shape of the mouse pointer when hovering over a component.

In addition, using this method it is still possible to enter more than four characters in the field, for example, pasting from the clipboard. If you want to block this, you will need to check if the entered text is longer than 4 characters, and if so, take only the first 4 characters.

+1
source

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 text field

+1
source

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

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

All Articles