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).
4 ответов
+ 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();
}
}
+ 1
KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent();
you can call this when length of text in the textfield is equal to 3.
+ 1
thank you my friend
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.