Click here to Skip to main content
15,885,953 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have jtextfiled which takes employee id as value.
Textfield Keytyped event does not let user enter anything other than numbers. Also it restrict user to enter only 7 digits. This works fine.
Now i want that once user enter complete id he hits enter key so that login\logout button will be enabled. Here i have problem.
When i have 7 digit id in my textfiled, Java does not recognize enter keypressed event and doesn't do anything. But if i have less than 7 digits, it works fine.
Here is my code

private void TxtFieldKeyTyped(java.awt.event.KeyEvent evt) {                                  
    int typedchar = evt.getKeyChar();
    if(!(Character.isDigit(typedchar)||
        typedchar== KeyEvent.VK_BACK_SPACE ||
        typedchar== KeyEvent.VK_DELETE) || TxtField.getCaretPosition() > 6){
            evt.consume();
    }
    
}

private void TxtFieldKeyPressed(java.awt.event.KeyEvent evt) {                                    
// TODO add your handling code here:

    if(evt.getKeyCode()==KeyEvent.VK_ENTER ){
        fieldValidate(); //FUnction to validate user input
    }
}


Please help me..as early as possible!
Thanks in adv!
Posted
Updated 2-Aug-13 22:34pm
v3

1 solution

Instead of handling the keyevent, try to use PlainDocument to put your logic.

http://www.example8.com/category/view/id/3662[^]
 
Share this answer
 
Comments
SanketAB 13-Aug-13 6:52am    
Thank you Subhash

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900