Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Limiting number of characters a user can enter in a JTextField - Netbeans?

807601May 22 2008 — edited May 24 2008
I have a form in Netbeans with a JTextField.
I want to limit the number of characters a user can enter into this field.

In my source code I have:

int strLen;
String fName = fName_text.getText(); //fName_text is the JTextField on my form.

if((strLen = fName.length()) >25){
JOptionPane.showMessageDialog(null,"FIRST NAME IS LIMITED TO 25 CHARACTERS","NOTICE",JOptionPane.INFORMATION_MESSAGE);}
else
{
//Stop the application and leave the focus on this field.
}

How do I write the code that will stop the application from running? ...and leave the focus on this JTextField so the user can enter the correct amount of characters?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 21 2008
Added on May 22 2008
24 comments
5,683 views