•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 423,501 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,748 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 481 | Replies: 8 | Solved
![]() |
•
•
Join Date: Aug 2007
Posts: 57
Reputation:
Rep Power: 2
Solved Threads: 0
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 That mask only allows up to 5 letters, so you'll have to add the others up to 25.
try {
MaskFormatter formatter = new MaskFormatter("?????");
txtMask.setFormatterFactory(new DefaultFormatterFactory(formatter));
txtMask.setFocusLostBehavior(JFormattedTextField.COMMIT);
} catch(ParseException ex) {
ex.printStackTrace();
}•
•
Join Date: Aug 2007
Posts: 57
Reputation:
Rep Power: 2
Solved Threads: 0
•
•
•
•
Try adding the MaskFormatter like soThat mask only allows up to 5 letters, so you'll have to add the others up to 25.try { MaskFormatter formatter = new MaskFormatter("?????"); txtMask.setFormatterFactory(new DefaultFormatterFactory(formatter)); txtMask.setFocusLostBehavior(JFormattedTextField.COMMIT); } catch(ParseException ex) { ex.printStackTrace(); }
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. trim() is a method of the String class that returns a copy of the String with leading and trailing spaces removed.
java Syntax (Toggle Plain Text)
String original = " blah "; String trimmed = original.trim(); System.out.println(trimmed);
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the Java Forum
- Previous Thread: I need Java or Ajax scroller (Div content)
- Next Thread: Simple Array Processing?



Linear Mode