User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Aug 2007
Posts: 57
Reputation: NycNessyness is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
NycNessyness NycNessyness is offline Offline
Junior Poster in Training

JFormattedTextField Alphabeticals

  #1  
May 19th, 2008
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;
	}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2007
Posts: 57
Reputation: NycNessyness is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
NycNessyness NycNessyness is offline Offline
Junior Poster in Training

Re: JFormattedTextField Alphabeticals

  #2  
May 20th, 2008
Think someone marked my question as solved but I'm still in need of help.
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,825
Reputation: Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all 
Rep Power: 12
Solved Threads: 282
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is online now Online
Posting Maven

Re: JFormattedTextField Alphabeticals

  #3  
May 20th, 2008
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.
Reply With Quote  
Join Date: Aug 2007
Posts: 57
Reputation: NycNessyness is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
NycNessyness NycNessyness is offline Offline
Junior Poster in Training

Re: JFormattedTextField Alphabeticals

  #4  
May 20th, 2008
Originally Posted by Ezzaral View Post
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!
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,825
Reputation: Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all 
Rep Power: 12
Solved Threads: 282
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is online now Online
Posting Maven

Re: JFormattedTextField Alphabeticals

  #5  
May 20th, 2008
"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.
Reply With Quote  
Join Date: Aug 2007
Posts: 57
Reputation: NycNessyness is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
NycNessyness NycNessyness is offline Offline
Junior Poster in Training

Re: JFormattedTextField Alphabeticals

  #6  
May 20th, 2008
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.
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,825
Reputation: Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all 
Rep Power: 12
Solved Threads: 282
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is online now Online
Posting Maven

Re: JFormattedTextField Alphabeticals

  #7  
May 20th, 2008
You may want to post the error message along with the updated code. No one here can see over your shoulder
Reply With Quote  
Join Date: Aug 2007
Posts: 57
Reputation: NycNessyness is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
NycNessyness NycNessyness is offline Offline
Junior Poster in Training

Re: JFormattedTextField Alphabeticals

  #8  
May 20th, 2008
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.
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,825
Reputation: Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all 
Rep Power: 12
Solved Threads: 282
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is online now Online
Posting Maven

Re: JFormattedTextField Alphabeticals

  #9  
May 21st, 2008
trim() is a method of the String class that returns a copy of the String with leading and trailing spaces removed.
  1. String original = " blah ";
  2. String trimmed = original.trim();
  3. System.out.println(trimmed);
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Java Forum

All times are GMT -4. The time now is 4:16 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC