In my application four TextArea is there and I want to enter only four character in one Text area and cursor automatically move to next TestArea. Again when I enter four character in this TextArea then again cursor automatically move to next TextArea.
Example: 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.
Same thing I want in my application.
For this first of all I add CustomizedTextFields.jar and then created four IntegerField:
private IntegerField text1;
private IntegerField text2;
private IntegerField text3;
private IntegerField text4;
after this I show all these IntegerField on my frame.
Now I tried this code to send cursor to the next field but it's not working:
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) {
}
});
interesting enough question to try improving my shadowy knowledge of the text package :-)
There are two separate requirements here
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);
}
The documentFilter is the one from the Swing Tutorial
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 the JTextComponents
, for listening add DocumentFilter
don't use KeyListener
for JTextComponents
, use only DocumentListener
add required next JTextArea
to the DocumentListener
, if is there typed 4th. Char into JTextArea
,
notice, moving with Focus
from one JTextArea
to another would be better wrapped into invokeLater
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With