-1

When I enter a value in the first JTextField it needs to be validated so that it should accept only alphabetical characters and not others. When I enter a character other than an alphabetical one, it should not be put into the JTextField, and it should give a message in the label format as "should enter alphabetical characters only" at the time of pressing other keys.

This is the code I used:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class valid extends JFrame  {
    perform1 per1=new perform1();
    String num1="Alpha";
    String num2="Numeric";
    String num3="Alphanumeric";
    Container contentPane = getContentPane();
    JPanel jp=new JPanel(new GridLayout(2, 2));
    JLabel label1=new JLabel("STUDENT NAME", JLabel.LEFT);
    JLabel label2=new JLabel("REG NO", JLabel.LEFT);
    JTextField text1=new JTextField(15);
    JTextField text2=new JTextField(15);
    public valid(){
        text1.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
                per1.dis(text1, e, num1);

            }
        });
        text2.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
                per1.dis(text2, e, num2);
            }
        });
        setLayout(new FlowLayout(FlowLayout.CENTER));
        jp.add(label1);
        jp.add(text1);
        jp.add(label2);
        jp.add(text2);
        contentPane.add("Center", jp);
        setSize(400, 300);
        setVisible(true);
    }
    public static void main(String ar[])
    {
        new valid();
    }
}
class perform1{
public void dis(JTextField PTxtCtrl,KeyEvent PKeyvalue,String PTempStr){
    JLabel error=new JLabel();
    //JTextField text=new JTextField();
    String TmpCurntStr;
    String TmpConvStr;
    value val = value.valueOf(PTempStr);

   switch(val){
       case Alpha:
           if((PKeyvalue.getKeyChar()>='a'&& PKeyvalue.getKeyChar()<='z')||
           (PKeyvalue.getKeyCode()==PKeyvalue.VK_DELETE)||
           (PKeyvalue.getKeyCode()==PKeyvalue.VK_BACK_SPACE))
           {
           TmpCurntStr=PTxtCtrl.getText();
           TmpConvStr=TmpCurntStr.toUpperCase();
           PTxtCtrl.setText(TmpConvStr);
           //PTxtCtrl.setEditable(true);
           //error.setText("");
          }else{
           //PTxtCtrl.setEditable(false);
           //error.setText("* Enter only numeric digits(0-9)");

           }
           break;
       case Numeric:
           if((PKeyvalue.getKeyChar()>='0'&& PKeyvalue.getKeyChar()<='9')||
           (PKeyvalue.getKeyCode()==PKeyvalue.VK_DELETE)||
           (PKeyvalue.getKeyCode()==PKeyvalue.VK_BACK_SPACE))
           {
           TmpCurntStr=PTxtCtrl.getText();
           System.out.println("hai");
           }else{
               JOptionPane.showMessageDialog(null,"Only numeric");
           }
           break;
       case Alphanumeric:
           break;
   }
 }
  enum value{
    Alpha,
    Numeric,
    Alphanumeric
}
}
Radiodef
  • 37,180
  • 14
  • 90
  • 125
arasicode
  • 23
  • 2
  • 6
  • trying again with the exact same question? http://stackoverflow.com/questions/8573365/i-didnt-get-a-proper-output-for-this-code-plz-send-me-a-correct-code – kleopatra Dec 21 '11 at 09:37
  • anyway (in case I didn't already :-) please learn java naming conventions and stick to them – kleopatra Dec 21 '11 at 09:39
  • 1
    I voted to close 3times same questions – mKorbel Dec 21 '11 at 09:41
  • 1
    Don't use KeyEvents as they are specific to AWT, use KeyBinding as you are using Swing. Just recently I got this reply when i posted an answer to Swing with KeyEvents, with -1 deduction from my score. It's better you learn about KeyBinding. Else someone might deduct your score too. Regards – nIcE cOw Dec 21 '11 at 09:42
  • ahh ... I _knew_ I have seen the exact same code yet another time: http://stackoverflow.com/questions/8572001/have-to-access-switchcase-statement-from-another-class-using-jtextfield - though the question changed slightly. How about reading your textbook? I'm sure it contains a chapter explaining how to solve that homework ... – kleopatra Dec 21 '11 at 09:44

3 Answers3

4

You can use JFormattedTextField or usual JTextField but assign your own DocumentFilter.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • 1
    by anonymous up-voter, I'm too late, I deleted same answer – mKorbel Dec 21 '11 at 09:39
  • My question is, when i am entering numeric key instead of alphabet at the time, the jtextfield is not supposed to accept to enter numeric key, then it will give a message in label box should be red color foreground is "Alpha only accept", this is next to that Particular Jtextfield. please give me solution for this. – arasicode Dec 21 '11 at 10:43
1

If you want to remove characters which are not alphabets you can use replaceAll to replace them with nothing. For example:

TmpCurntStr=PTxtCtrl.getText();
TmpConvStr=TmpConvStr.replaceAll("[^a-zA-Z]", "");
PTxtCtrl.setText(TmpConvStr);
dogbane
  • 266,786
  • 75
  • 396
  • 414
1

i have correct this check it if you don't understand then let me know.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class valid extends JFrame  {
    perform1 per1=new perform1();
    String num1="Alpha";
    String num2="Numeric";
    String num3="Alphanumeric";
    Container contentPane = getContentPane();
    JPanel jp=new JPanel(new GridLayout(2, 2));
    JLabel label1=new JLabel("STUDENT NAME", JLabel.LEFT);
    JLabel label2=new JLabel("REG NO", JLabel.LEFT);
    JTextField text1=new JTextField(15);
    JTextField text2=new JTextField(15);
    public valid(){
        text1.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
                /**
                 * check ASCII value of character enter in text1
                 * if it not fall in alphabets then send Alphanumeric
                 * 
                 */
                char text = e.getKeyChar();
                if(text >= 65 && text <= 90 || text >= 97 && text <= 122){
                    per1.dis(text1, e, num1);
                }else{
                    per1.dis(text1, e, num3);
                }

            }
        });
        text2.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
                per1.dis(text2, e, num2);
            }
        });
        setLayout(new FlowLayout(FlowLayout.CENTER));
        jp.add(label1);
        jp.add(text1);
        jp.add(label2);
        jp.add(text2);
        contentPane.add("Center", jp);
        setSize(400, 300);
        setVisible(true);
    }
    public static void main(String ar[])
    {
        new valid();
    }
}
class perform1{
public void dis(JTextField PTxtCtrl,KeyEvent PKeyvalue,String PTempStr){
    JLabel error=new JLabel();
    //JTextField text=new JTextField();
    String TmpCurntStr;
    String TmpConvStr;
    value val = value.valueOf(PTempStr);

   switch(val){
       case Alpha:
           if((PKeyvalue.getKeyChar()>='a'&& PKeyvalue.getKeyChar()<='z')||
           (PKeyvalue.getKeyCode()==PKeyvalue.VK_DELETE)||
           (PKeyvalue.getKeyCode()==PKeyvalue.VK_BACK_SPACE))
           {
           TmpCurntStr=PTxtCtrl.getText();
           TmpConvStr=TmpCurntStr.toUpperCase();
           PTxtCtrl.setText(TmpConvStr);
           //PTxtCtrl.setEditable(true);
           //error.setText("");
          }else{
           //PTxtCtrl.setEditable(false);
           //error.setText("* Enter only numeric digits(0-9)");

           }
           break;
       case Numeric:
           if((PKeyvalue.getKeyChar()>='0'&& PKeyvalue.getKeyChar()<='9')||
           (PKeyvalue.getKeyCode()==PKeyvalue.VK_DELETE)||
           (PKeyvalue.getKeyCode()==PKeyvalue.VK_BACK_SPACE))
           {
           TmpCurntStr=PTxtCtrl.getText();
           System.out.println("hai");
           }else{
               JOptionPane.showMessageDialog(null,"Only numeric");
               PTxtCtrl.setText("");
           }
           break;
       case Alphanumeric:
//         System.err.println("check");
           /**
            * if it send Alphanumeric then give a pop up message
            */
           JOptionPane.showMessageDialog(null,"Only Alphabets");
           PTxtCtrl.setText("");
           break;
   }
 }
  enum value{
    Alpha,
    Numeric,
    Alphanumeric
}
}
Ashish
  • 1,527
  • 4
  • 17
  • 33