Hello. I asked last week about how to allow the user to only enter in letters. I tried the mask formatter but that's too limited. How can I go about letting the user to enter in any kind of letters they want but restricted to only 25 letters for their name. The user shouldn't be allowed to enter anything that is not a letter. This is an example of the code that I have tried that came close, but the user has to enter 15 letters and no less, which is irritating. May someone please assist me on this. Thanks in advance!

JFormattedTextField lastName= new JFormattedTextField(MaskFormat());

	public MaskFormatter MaskFormat()
    {
   		MaskFormatter nameFormat = null;

   		try
   		{

   			nameFormat = new MaskFormatter("ULLLLLLLLLLLLL");
			

   		}
   		catch (java.text.ParseException exc)
   		{

		}

		return nameFormat;
	}

Try adding the MaskFormatter like so

try {
    MaskFormatter formatter = new MaskFormatter("?????");
    txtMask.setFormatterFactory(new DefaultFormatterFactory(formatter));
    txtMask.setFocusLostBehavior(JFormattedTextField.COMMIT);
} catch(ParseException ex) {
    ex.printStackTrace();
}

That mask only allows up to 5 letters, so you'll have to add the others up to 25.

Try adding the MaskFormatter like so

try {
    MaskFormatter formatter = new MaskFormatter("?????");
    txtMask.setFormatterFactory(new DefaultFormatterFactory(formatter));
    txtMask.setFocusLostBehavior(JFormattedTextField.COMMIT);
} catch(ParseException ex) {
    ex.printStackTrace();
}

That mask only allows up to 5 letters, so you'll have to add the others up to 25.

I tried the above code but couldn't get it to work properly. Is txtMask required or is that suppose to be the name of my JFormattedTextField? If so I'm still getting an error when I compile. How can I use my code to implement the one you posted properly. Thanks again!

"txtMask" was simply the name of the JFormattedTextField that I was using in a small test file. In your case that would be "lastName". You need to call setFocusLostBehavior(JFormattedTextField.COMMIT) on it so it does not discard the field input when the string is shorter than your mask. You will also need to call trim() on the result of getText(), because it will contain placeholder spaces padded to the length of the mask.

Sorry to bother you again. I've never used the trim method. How can I get that to work with the code you posted, as an example. I've implemented all the code and it compiles well but when I run the program I get the error message and can't make it to the program.

You may want to post the error message along with the updated code. No one here can see over your shoulder :P

I apologize. I got it to work and found out what my problem was lol, but how would I use the trim() method, just for the extra knowledge. I really appreciate the help, I've been stuck on the name part for about a week and now you've saved me.

trim() is a method of the String class that returns a copy of the String with leading and trailing spaces removed.

String original = "  blah  ";
String trimmed = original.trim();
System.out.println(trimmed);
This question has already been answered. Start a new discussion instead.