Hello Friends,

I want to set the length of the textfield in java...

Please check my below code....it works finely if I press the keys one by one slowly...
But suppose if I press any key for a longer time the actual length exceeds and so the validation is not done correctly...

Can anyone help me to set the length??

public class TextValidation extends KeyAdapter
    {
        public void keyReleased(KeyEvent key)
        {
            Object txt=key.getSource();
            if(txt==txtPatientId)
            {
                char ch=key.getKeyChar();       
                String specialchar="!@#$%^&*()~?>'<:{}|+_/\".,;'][=-` \\";
                if (txtPatientId.getText().trim().length() <= 10)
                {
                    if (specialchar.indexOf(ch)>-1)
                    {
                        JOptionPane.showMessageDialog(null, "SPECIAL CHARACTER IS NOT ALLOWED","Title",JOptionPane.WARNING_MESSAGE);
                        txtPatientId.setText(txtPatientId.getText().substring(0, txtPatientId.getText().length()-1));
                    }
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "MAXIMUM 10 CHARACTERS","Title",JOptionPane.WARNING_MESSAGE);
                    txtPatientId.setText(txtPatientId.getText().substring(0, txtPatientId.getText().length()-1));       
                }
            }
        }
    }

Recommended Answers

All 5 Replies

You have to change your key listener from Released to Pressed. Reason is on release it check so your characters go out of range. Hope this help.

 public void keyPressed(KeyEvent key)

i think the suggestion given by Majestics is correct.

I have created a class as below

public class MyTextField extends JTextField implements KeyListener
{
int size;
    public MyTextField(int size)
    {
        super(15);
        this.size=size;
        addKeyListener(this);
    }
    public void keyTyped(KeyEvent e)
    {   
        char ch=e.getKeyChar();     
        String specialchar="!@#$%^&*()~?>'<:{}|+_/\".,;'][=-` \\";

        if (specialchar.indexOf(ch)>-1)
        {
            JOptionPane.showMessageDialog(null, "SPECIAL CHARACTER IS NOT ALLOWED","",JOptionPane.WARNING_MESSAGE);
            e.setKeyChar('\0');
        }
        if(getText().length()>size-1)
        e.setKeyChar('\0');

    }   
    public void keyPressed(KeyEvent e){}
    public void keyReleased(KeyEvent e){}
}

And then called the textfield as follows

MyTextField txtPatientID =new MyTextField(15);

not never to use KeyListener for JTextComponents, wrong way, have look at DocumentFilter for special Chars to use Pattern

mKorbel's advice is good. Unlike KeyListener, a DocumentListener will correctly deal with copy/paste and programatic changes to the text. as well as anything the user types

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.