0

How to go next jtextfield after typing third digit?

I have a jFormattedTextField that can write a number with 3 digits as input, For example 328. I want the next jtextfield getting focused when i insert third digit(8).

14th Oct 2018, 4:54 PM
hamid
hamid - avatar
4 odpowiedzi
+ 2
//Here is a short example i wrote to illustrate how it gets done. import javax.swing.*; import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; public class Temp extends JFrame { Temp(){ super("Switch"); JTextField one = new JTextField(3); JTextField two = new JTextField(5); setLayout(new FlowLayout()); add(new JLabel("One:")); add(one); one.addKeyListener(new KeyAdapter() { @Override public void keyTyped(KeyEvent e) { if(one.getText().length()==2) KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent(); } }); add(new JLabel("Two:")); add(two); setVisible(true); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); } public static void main(String[] args) { new Temp(); } }
15th Oct 2018, 6:50 AM
Tanay
Tanay - avatar
+ 1
KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent(); you can call this when length of text in the textfield is equal to 3.
14th Oct 2018, 8:16 PM
Tanay
Tanay - avatar
+ 1
thank you my friend
15th Oct 2018, 7:38 PM
hamid
hamid - avatar
0
which event should use? pressed, typed or released? i try do it for all of them(pressed, typed or released) but dont worked. if(textfield.gettext().lenght()==3){ KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent(); } with each press get to the next component.
15th Oct 2018, 5:49 AM
hamid
hamid - avatar