Capturing all type of inputs in the JTextField using java swing - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Capturing all type of inputs in the JTextField using java swing

sjohnfernandassjohnfernandas Posts: 2Member
Hi i used 4 JTextField in my swing form.i need to do is moving the focus from one JTextField to the other JTextField by hit the enter key and tab key.i done it successfully but the problem was the JTextField allows only numeric values if i give any value other than numeric value in the JTextfield the JTextField is locked and there is no focus transfer happen between the JTextFields.the code i was used

package focus;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter.*;
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 implements KeyListener{
JTextField txt1 = new JTextField(20);
JTextField txt2 = new JTextField(20);
JTextField txt3 = new JTextField(20);
JTextField txt4 = new JTextField(20);
JFrame f = new JFrame();
public Main()
{

txt1.addFocusListener(new MyFocusListener());
txt2.addFocusListener(new MyFocusListener());
txt3.addFocusListener(new MyFocusListener());
txt4.addFocusListener(new MyFocusListener());
txt1.addKeyListener(this);
txt2.addKeyListener(this);
txt3.addKeyListener(this);
txt4.addKeyListener(this);
f.setLayout(new FlowLayout());
f.add(txt1);
f.add(txt2);
f.add(txt3);
f.add(txt4);
f.pack();
f.setVisible(true);}

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

}

public void keyTyped(KeyEvent e) {

}

public void keyPressed(KeyEvent ke) {
if (ke.getSource() == txt1)
{
if (ke.getKeyCode() == KeyEvent.VK_ENTER)
{
txt2.requestFocusInWindow();
txt2.selectAll();
}
}
else if (ke.getSource() == txt2)

{
if (ke.getKeyCode() == KeyEvent.VK_ENTER)
{
txt3.requestFocusInWindow();
txt3.selectAll();
}
}
else if (ke.getSource() == txt3)
{
if (ke.getKeyCode() == KeyEvent.VK_ENTER)
{
txt4.requestFocusInWindow();
txt4.selectAll();
}
}
else if (ke.getSource() == txt4)
{
if (ke.getKeyCode() == KeyEvent.VK_ENTER)
{
txt1.requestFocusInWindow();
txt1.selectAll();
}
}

}

public void keyReleased(KeyEvent e) {

}
}
class MyFocusListener 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;
}
}
}

}


Sign In or Register to comment.