We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,650 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Set the length of the text field

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));       
                }
            }
        }
    }
5
Contributors
5
Replies
3 Days
Discussion Span
8 Months Ago
Last Updated
7
Views
Question
Answered
poojavb
Posting Pro
524 posts since Nov 2011
Reputation Points: 39
Solved Threads: 77
Skill Endorsements: 7

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)
Majestics
Practically a Master Poster
696 posts since Jul 2007
Reputation Points: 209
Solved Threads: 66
Skill Endorsements: 5

i think the suggestion given by Majestics is correct.

jalpesh_007
Posting Whiz
350 posts since Sep 2010
Reputation Points: 4
Solved Threads: 37
Skill Endorsements: 5
Question Answered as of 8 Months Ago by Majestics and jalpesh_007

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);
poojavb
Posting Pro
524 posts since Nov 2011
Reputation Points: 39
Solved Threads: 77
Skill Endorsements: 7

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

mKorbel
Nearly a Posting Virtuoso
1,228 posts since Feb 2011
Reputation Points: 482
Solved Threads: 244
Skill Endorsements: 14

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

JamesCherrill
... trying to help
Moderator
8,667 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,477
Skill Endorsements: 33

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0745 seconds using 2.74MB