Java

Moderators: zibadian
Number of threads: 7836
Number of posts: 18235

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Capturing all type of inputs in the JTextField using java swing Posted by sjohnfernandas on 21 Dec 2011 at 3:25 AM
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<JTextField>());
txt2.addFocusListener(new MyFocusListener<JTextField>());
txt3.addFocusListener(new MyFocusListener<JTextField>());
txt4.addFocusListener(new MyFocusListener<JTextField>());
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 <C extends JTextField> 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;
}
}
}

}





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.